Struts2 实现单/多文件上传(二)

2014-11-24 02:22:06 · 作者: · 浏览: 1
= ServletActionContext.getServletContext().getRealPath(""); // 获取项目根路径
5 //文件路径
6 String path = absolutePath + "/" + this.savePath + "/";
7 //创建路径,如果目录不存在,则创建
8 File file = new File(path);
9 if(!file.exists()) {
10 file.mkdirs();
11 }
12 //文件路径+文件名
13 path +=fileUploadFileName.get(i);
14 //1.构建输入流
15 FileInputStream fis = new FileInputStream(f);
16 //2.构建输出流
17 FileOutputStream fos = new FileOutputStream(path);
18 //3.通过字节写入输出流
19 try {
20 byte[] buf = new byte[1024];
21 int len = 0;
22 while ((len = fis.read(buf)) > 0) {
23 fos.write(buf, 0, len);
24 }
25 } catch (Exception e) {
26 e.printStackTrace();
27 } finally {
28 fis.close();
29 fos.close();
30 }
31 i++;
32 }
33 return SUCCESS;
34 }