public class UploadAction extends ActionSupport {
private File[] file;
private String[] fileFileName;
private String[] fileContentType;
private String uploadPath;
// 省略各Field的getter、setter方法
public String toUpload() throws Exception {
for (int i = 0; i < file.length; i++) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 将临时文件写入到指定文件
fis = new FileInputStream(file[i]);
fos = new FileOutputStream(getRealFilename(fileFileName[i]));
byte[] buffer = new byte[1024];
int len = -1;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
return INPUT;
} finally {
// 释放资源
if (fis != null)
fis.close();
if (fos != null)
fos.close();
if (file[i] != null)
file[i].delete();
}
}
return SUCCESS;
}
Struts2文件上传 —— 多个文件上传
最新推荐文章于 2017-11-28 22:45:30 发布