`
侯上校
  • 浏览: 216925 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

文件上传【一个文件或者多文件的上传】

 
阅读更多
文件上传:
第一步:加jar 包, commons-fileupload-1.2.1.jar, commons-io-1.3.2.jar
第二步:把form enctype属性设为"application/x-www-form-urlencoded"
<form action="${pageContext.request.contextPath }/xxx.action" method="post" enctype="multipart/form-data">
    <input type="file" name="uploading"/>
</form>
第三步:action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:
              private File image;//得到上传的文件
   private String imageFileName;//得到文件的名称:前面image就上传文件名称,,
后面是固定写法
              private String  imageContentType;//得到上传文件的类型
//上面的三个属性都是固定的写法,image是上传表单中name,其它的是固定写法
              String realPath=ServletActionContext.getServletContext().getRealPath("/image");
System.out.println("真实路经:"+realPath);
System.out.println("文件名称:"+imageFileName);
if(image!=null){
File savefile=new File(new File(realPath),imageFileName);
if(!savefile.getParentFile().exists())savefile.getParentFile().mkdirs();
//image是源文件,后面是要把它放到那个目标路经,
FileUtils.copyFile(image, savefile);
ActionContext.getContext().put("msg", "文件上传成功!!");
}

多文件上传
如果要上传多个文件,
○1>form表单里的事name相同
○2>把action里的属性设为数组
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics