1 upload.jsp
2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
3 <%@taglib uri="/struts-tags" prefix="s" %>
4 <%
5 String path = request.getContextPath();
6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
7 %>
8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9 <html>
10 <head>
11 <base href="<%=basePath%>">
12 <title>index.jsp</title>
13 <meta http-equiv="pragma" content="no-cache">
14 <meta http-equiv="cache-control" content="no-cache">
15 </head>
16 <body>
17 <div>
18 <s:form action="Fileupload.action" method="Post" name="form" theme="simple" enctype="multipart/form-data">
19 <s:textfield label="文件标题" name="title">文件标题</s:textfield> <br/>
20 <s:file label="文件路径" name="upload">文件路径</s:file></br>
21 <s:submit label="保存" name="submit" value="保存"></s:submit>
22 </s:form>
23 </div>
24 </body>
25 </html>
26
struts.xml
<action name="Fileupload" class="com.action.FileuploadAction">
<interceptor-ref name="fileUpload">
<param name="allowType">image/bmp,image/png,image/gif,image/jpeg,image/jpg/image/x-png,image/pjpeg</param>
<param name="maximumSize">10240000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="success">/showupload.jsp</result>
<param name="savePath">/upload</param>
<result name="input">/upload.jsp</result>
</action>
1 FileUploadAction.java
2
3 package com.action;
4
5 import java.io.BufferedInputStream;
6 import java.io.BufferedOutputStream;
7 import java.io.File;
8 import java.io.FileInputStream;
9 import java.io.FileOutputStream;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12
13 import org.apache.struts2.ServletActionContext;
14
15 import com.opensymphony.xwork2.ActionSupport;
16
17 public class FileuploadAction extends ActionSupport{
18
19 private static final int BUFFER_SIZE = 16*1024;
20 private String title;
21 private File upload; //上传文件域对象
22 private String uploadFileName; //上传文件名
23 private String uploadContentType; //上传文件类型
24 private String savePath;//保存文件的目录路径
25 public String getTitle() {
26 return title;
27 }
28 public void setTitle(String title) {
29 this.title = title;
30 }
31 public File getUpload() {
32 return upload;
33 }
34 public void setUpload(File upload) {
35 this.upload = upload;
36 }
37 public String getUploadFileName() {
38 return uploadFileName;
39 }
40 public void setUploadFileName(String uploadFileName) {
41 this.uploadFileName = uploadFileName;
42 }
43 public String getUploadContentType() {
44 return uploadContentType;
45 }
46 public void setUploadContentType(String uploadContentType) {
47 this.uploadContentType = uploadContentType;
48 }
49 public String getSavePath() {
50 return savePath;
51 }
52 public void setSavePath(String savePath) {
53 this.savePath = savePath;
54 }
55 public static int getBufferSize() {
56 return BUFFER_SIZE;
57 }
58
59 public String execute() throws Exception{
60 //根据服务器的文件保存地址和原文件名,创建目录文件全路径
61 String dstPath = ServletActionContext.getServletContext().getRealPath(this.getSavePath())+"\\"+ this.getUploadFileName();
62 System.out.println("上传文件的类型=="+this.getUploadContentType());
63 File dstFile = new File(dstPath);
64 copy(this.upload,dstFile);
65
66 return "success";
67 }
68 //文件拷贝方法
69 public void copy(File src ,File dst){
70 InputStream in = null;
71 OutputStream out = null;
72 try {
73 in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE);
74 out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);
75
76 byte[] buffer =new byte[BUFFER_SIZE];
77 int len = 0;
78 while((len = in.read(buffer))>0){
79 out.write(buffer,0,len);
80 }
81 } catch (Exception e) {
82 e.printStackTrace();
83 }finally{
84 if(null!=in){
85 try{
86 in.close();
87 }catch (Exception e) {
88 e.printStackTrace();
89 }
90 }
91 if(null!=out){
92 try{
93 out.close();
94 }catch (Exception e) {
95 e.printStackTrace();
96 }
97 }
98
99 }
100
101 }
102
103 }
104
1 showupload.jsp
2
3 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
4 <%@taglib uri="/struts-tags" prefix="s" %>
5 <%
6 String path = request.getContextPath();
7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
8 %>
9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11 <head>
12 <base href="<%=basePath%>">
13 <title>index.jsp</title>
14 <meta http-equiv="pragma" content="no-cache">
15 <meta http-equiv="cache-control" content="no-cache">
16 </head>
17 <body>
18 <div>
19 <img alt="image" src='upload/<s:property value="uploadFileName"/>' />
20 <br/>
21 <s:property value="uploadFileName" />
22 </div>
23 </body>
24 </html>
25