码迷,mamicode.com
首页 > Web开发 > 详细

Struts2文件上传

时间:2015-12-30 00:08:49      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

 1  在Struts2中上传文件需要 commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar 这两个包。

 2  确认页面form表单上的提交方式为POST,enctype属性的值为“multipart/form-data”。

fileupload.jsp页面:

1 <body>
2        <form action="<%=basePath%>lixiang/file_upload.action" method="post" enctype="multipart/form-data">
3            选择文件:<input type="file" name="upload"/><br/>
4           <input type="submit" value="提交"/>
5        </form>
6   </body>
FileUploadAction.java
 1 package com.cy.action;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 
 9 import com.opensymphony.xwork2.ActionSupport;
10 
11 public class FileUploadAction extends ActionSupport{
12 
13     private static final long serialVersionUID = 1L;
14     private File upload;
15     //获取上传文件类型
16     private String uploadContentType;
17     //获取上传文件名
18     private String uploadFileName;
19     //保存路径
20     private String savePath;
21     
22     public String upload(){
23     
24         File dir = new File(savePath);
25         //先判断文件夹是否存在
26         if(!dir.exists()){
27             dir.mkdir();
28         }
29         
30         //再判断文件是否已经存在
31         String filePath = savePath + "\\"+uploadFileName;
32         File file = new File(filePath);//上传后的目标文件
33         FileOutputStream out = null;
34         FileInputStream in = null;
35         if(!file.exists()){
36             try {
37                 out = new FileOutputStream(filePath);
38                 in = new FileInputStream(upload);
39                 
40                 byte[] bytes = new byte[1024];
41                 int lenth = 0;
42                 while ((lenth = in.read(bytes)) > 0) {
43                     out.write(bytes, 0, lenth);
44                 }
45             
46             } catch (FileNotFoundException e) {
47                 e.printStackTrace();
48             } catch (IOException e) {
49                 e.printStackTrace();
50             }finally{
51                 try {
52                     in.close();
53                     out.close();
54                 } catch (IOException e) {
55                     e.printStackTrace();
56                 }
57             }
58         }
59         
60         return SUCCESS;
61     }
62 
63 
64 // 提供set、get方法
65     public File getUpload() {
66         return upload;
67     }
68     public void setUpload(File upload) {
69         this.upload = upload;
70     }
71     public String getUploadContentType() {
72         return uploadContentType;
73     }
74     public void setUploadContentType(String uploadContentType) {
75         this.uploadContentType = uploadContentType;
76     }
77     public String getUploadFileName() {
78         return uploadFileName;
79     }
80     public void setUploadFileName(String uploadFileName) {
81         this.uploadFileName = uploadFileName;
82     }
83     public String getSavePath() {
84         return savePath;
85     }
86     public void setSavePath(String savePath) {
87         this.savePath = savePath;
88     }
89     
90     
91 }

struts.xml

1 <action name="file_*" class="com.cy.action.FileUploadAction" method="{1}">
2             <param name="savePath">E:\images</param>  <!-- 设置上传文件存放的路径  -->
3             <result name="success">/layout.jsp</result> 
4             <result name="input">/fileupload.jsp</result> 
5         </action>

 

Struts2文件上传

标签:

原文地址:http://www.cnblogs.com/hellokitty1/p/5087363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!