读书人

struts2资料上传upload

发布时间: 2013-04-09 16:45:09 作者: rapoo

struts2文件上传,upload

在后台文件中:

private File image; ? //文件

private String imageFileName; ? //文件名称

public String upload(){

? ? String realPath=ServletActionContext.getServletContext().getRealPath("/imag");

? ? File file=new File(realPath);

? ??if(!file.exists()){

? ??file.mkdirs();

}

try {

? ??FileUtils.copyFile(image, new File(file,imageFileName)); ? //copy到imag文件中

} catch (IOException e) {

? ??// TODO Auto-generated catch block

? ??e.printStackTrace();

}

? ??return "success";

}

?

public File getImage() {

? ??return image;

}

public void setImage(File image) {

? ??this.image = image;

}

public String getImageFileName() {

? ??return imageFileName;

}

public void setImageFileName(String imageFileName) {

? ??this.imageFileName = imageFileName;

}

?

jsp页面中:

<form action="user/user_upload.action" method="post" enctype="multipart/form-data">?

? ??<input type="file" name="image" border="1"/> <input type="submit" value="上传"/>

?</form>

?

?

这样就可以实现简单的文件上传了!

读书人网 >开源软件

热点推荐