`
elvajxw
  • 浏览: 107194 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

用struts2上传文件

阅读更多

在jsp页面:在form表单中一定要加上enctype="multipart/form-data"
     上传文件:<input type="file" name="upload"/> 
  在action页面:
   private File upload;
  private String uploadContentType;//文件类型注意此名的前半部分一定要和jsp中的file 的name一样
  private String uploadFileName;
  private String uploadSavepath="\\upload\\exam"; 
  public String getUploadSavepath() {
  return ServletActionContext.getServletContext().getRealPath(this.uploadSavepath);
  }
  public void setUpload(File upload) {
   this.upload = upload;
  }
  public void setUploadContentType(String uploadContentType) {
   this.uploadContentType = uploadContentType;
  }
  public void setUploadFileName(String uploadFileName) {
   this.uploadFileName = uploadFileName;
  }
  
  public String uploadfile(){
   if(this.upload != null){
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
    String date = dateFormat.format(new Date());
    int simple = new Random().nextInt(1000);
    this.filename = date + simple + this.uploadFileName.substring(this.uploadFileName.indexOf("."));
    String bookAllName = this.getUploadSavepath() + File.separator + filename;
    File  file = new File(bookAllName);
    this.upload.renameTo(file);
   }
   return "uploadfile";
  }
  ok了!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics