struts2单文件上传案例演示(一)
2016-11-28 13:55
155 查看
public class UploadAction1 extends ActionSupport implements Serializable { private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的 private String imageFileName;// 上传输入域FileName 文件名 private String imageContentType;// 上传文件的MIME类型 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; }
public String getImageContentType() { return imageContentType; }
public void setImageContentType(String imageContentType) { this.imageContentType = imageContentType; }
public String execute(){ System.out.println(imageContentType); try { //处理实际的上传代码 //找到存储文件的真实路径// System.out.println(imageFileName); ServletContext sc = ServletActionContext.getServletContext(); String storePath = sc.getRealPath("/files"); //构建输入输出流// OutputStream out = new FileOutputStream(storePath+"\\"+imageFileName);// InputStream in = new FileInputStream(image);// byte b[] = new byte[1024];// int len = -1;// while((len=in.read(b))!=-1){// out.write(b, 0, len);// }// out.close();// in.close(); FileUtils.copyFile(image, new File(storePath,imageFileName)); ActionContext.getContext().put("message", "上传成功!"); return SUCCESS; } catch (Exception e) { e.printStackTrace(); return ERROR; } }}
public void setImage(File image) { this.image = image; }
public String getImageFileName() { return imageFileName; }
public void setImageFileName(String imageFileName) { this.imageFileName = imageFileName; }
public String getImageContentType() { return imageContentType; }
public void setImageContentType(String imageContentType) { this.imageContentType = imageContentType; }
public String execute(){ System.out.println(imageContentType); try { //处理实际的上传代码 //找到存储文件的真实路径// System.out.println(imageFileName); ServletContext sc = ServletActionContext.getServletContext(); String storePath = sc.getRealPath("/files"); //构建输入输出流// OutputStream out = new FileOutputStream(storePath+"\\"+imageFileName);// InputStream in = new FileInputStream(image);// byte b[] = new byte[1024];// int len = -1;// while((len=in.read(b))!=-1){// out.write(b, 0, len);// }// out.close();// in.close(); FileUtils.copyFile(image, new File(storePath,imageFileName)); ActionContext.getContext().put("message", "上传成功!"); return SUCCESS; } catch (Exception e) { e.printStackTrace(); return ERROR; } }}
相关文章推荐
- struts2单文件上传案例演示(二)
- struts2单文件上传案例演示(一)
- struts2单文件上传案例演示(二)
- struts多文件上传案例演示(一)
- struts多文件上传案例演示(一)
- flex struts2单文件上传、同名文件上传、多文件上传
- struts2单文件,多文件上传
- Struts2单文件、多文件上传
- struts2单文件与多文件上传
- myeclipse上传文件案例的struts.xml配置文件
- struts2单文件和多文件的上传
- struts2.X心得9--struts2自定义拦截器以及文件上传等几个拦截器使用案例
- Struts2单文件上传
- Struts2单文件上传和多文件上传两种类型
- struts2单文件上传
- Struts2单文件上传
- struts2单文件和多文件的上传和下载
- struts2单文件上传
- Struts2单文件上传