JavaStruts2文件上传-单文件上传,多文件上传
Posted to-red
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaStruts2文件上传-单文件上传,多文件上传相关的知识,希望对你有一定的参考价值。
- 单文件上传
表单:
<form action="upload1.action" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="name" id="name"><br> 照片:<input type="file" name="photo"> <br> <input type="submit" value="提交"> </form>
action:
package com.hj.action; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; public class FileUploadNormal1 private String name; // 表单中的name private File photo; // 表单中的photo private String photoFileName; // 表单中文件的名字+FileName,如果文件属性名为myPhoto,则此处为myPhotoFileName private String photoContentType; // +ContentType public String execute() throws IOException System.out.println(this.photoFileName); System.out.println(this.photoContentType); File destFile = new File("C:\\File_rec\\tmp\\"+photoFileName); // 上传到的路径
// File destFile = new File(ServletActionContext.getServletContext().getRealPath("/upload/"+photoFileName)); //项目路径 FileUtils.copyFile(photo,destFile); return "success"; // getter,setter public String getName() return name; public void setName(String name) this.name = name; public File getPhoto() return photo; public void setPhoto(File photo) this.photo = photo; public String getPhotoFileName() return photoFileName; public void setPhotoFileName(String photoFileName) this.photoFileName = photoFileName; public String getPhotoContentType() return photoContentType; public void setPhotoContentType(String photoContentType) this.photoContentType = photoContentType;
- 多文件上传
上传多个文件只需要将文件相关的属性,改为数组形式即可
表单:
<form action="upload2.action" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="name" id="name"><br> 照片:<input type="file" name="photo"> <br> 照片:<input type="file" name="photo"> <br> 照片:<input type="file" name="photo"> <br> <input type="submit" value="提交"> </form>
多文件上传的action:
package com.hj.action; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; public class FileUploadNormal2 private String name; // 表单中的name private File[] photo; // 表单中的photo private String[] photoFileName; // 表单中文件的名字+FileName,如果文件属性名为myPhoto,则此处为myPhotoFileName private String[] photoContentType; // +ContentType public String execute() throws IOException for (int i = 0; i < photo.length; i++) System.out.println(this.photoFileName[i]); System.out.println(this.photoContentType[i]); File destFile = new File("C:\\File_rec\\tmp\\"+photoFileName[i]); FileUtils.copyFile(photo[i],destFile); return "success"; // getter,setter public String getName() return name; public void setName(String name) this.name = name; public File[] getPhoto() return photo; public void setPhoto(File[] photo) this.photo = photo; public String[] getPhotoFileName() return photoFileName; public void setPhotoFileName(String[] photoFileName) this.photoFileName = photoFileName; public String[] getPhotoContentType() return photoContentType; public void setPhotoContentType(String[] photoContentType) this.photoContentType = photoContentType;
以上是关于JavaStruts2文件上传-单文件上传,多文件上传的主要内容,如果未能解决你的问题,请参考以下文章
SpringMVC单文件上传多文件上传文件列表显示文件下载(转)