首页 > 技术文章 > Struts上传文件

lihuibin 2017-12-14 14:46 原文

Struts上传文件分为两个步骤:

1). 首先将客户端上传的文件保存到Struts.multipart.saveDir键所指定的目录中,如果该键所对应的目录不存在,那么就保存到javax.servlet.context.tempdir环境变量所指定的目录中。

2). Action中所定义的File类型的成员变量实际上是指向的是临时目录中的临时文件,然后在服务器端通过IO的方式将临时文件写入到指定的服务器端目录中

环境搭建:

首先看一下目录结构:

在对应的文件夹下创建以上图片中的文件

1、在com.action中新建文件上传的action,

 1 package com.action;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.InputStream;
 7 import java.io.OutputStream;
 8 
 9 import org.apache.struts2.ServletActionContext;
10 
11 import com.opensymphony.xwork2.ActionSupport;
12 
13 @SuppressWarnings("serial")
14 public class UploadAction extends ActionSupport {
15 
16     private String username; //用户名字
17     private File file;    //上传的文件
18     private String fileFileName;    //获取文件名字,命名格式固定为XXXFileName
19     private String fileContentType;    //获取文件的内容类型格式固定为XXXContentType
20     public String getFileContentType() {
21         return fileContentType;
22     }
23     public void setFileContentType(String fileContentType) {
24         this.fileContentType = fileContentType;
25     }
26     public String getFileFileName() {
27         return fileFileName;
28     }
29     public void setFileFileName(String fileFileName) {
30         this.fileFileName = fileFileName;
31     }
32     public File getFile() {
33         return file;
34     }
35     public void setFile(File file) {
36         this.file = file;
37     }
38     public String getUsername() {
39         return username;
40     }
41     public void setUsername(String username) {
42         this.username = username;
43     }
44     @Override
45     public String execute() throws Exception {
46         //获取目标文件的路径
47         String root = ServletActionContext.getRequest().getRealPath("/upload");
48         //创建文件输入流
49         InputStream input = new FileInputStream(file);
50         
51         System.out.println("fileFileName: " + fileFileName);
52         //在root目录下创建一个名字为fileFileName的文件
53         File destFile = new File(root,fileFileName);
54         //上传的文件输出到destFile文件中
55         OutputStream out = new FileOutputStream(destFile);
56         
57         byte[] buffer = new byte[1024];
58          
59         int length = 0;
60          
61         while((length = input.read(buffer)) != -1){
62             out.write(buffer,0,length);
63         }
64         
65         input.close();
66         out.close();
67         
68         return SUCCESS;
69     }
70 }

2、在struts.properties属性文件中指定缓冲临时文件的临时目录的路径

struts.multipart.saveDir =E\:/programTempFile

需要在E盘的根目录下创建一个programTempFile文件

3、fileUpload.jsp页面代码

 1 <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'fileUpload.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     <!-- 文件上传表单的提交方式必须是“post” 编码类型必须为:enctype="multipart/form-data" -->
27     <form action="fileUpload.action" method="post" enctype="multipart/form-data">
28     
29         username: <input type="text" name="username" /><br>
30         file:  <input type="file" name="file"><br>
31         <input type="submit" value="上传文件">
32     
33     </form>
34     
35   </body>
36 </html>

fileUploadResult.jsp代码

 1 <%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GB18030"%>
 2 <%@ taglib prefix="s" uri="/struts-tags" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'fileUploadResult.jsp' starting page</title>
14     
15     <meta http-equiv="pragma" content="no-cache">
16     <meta http-equiv="cache-control" content="no-cache">
17     <meta http-equiv="expires" content="0">    
18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19     <meta http-equiv="description" content="This is my page">
20     <!--
21     <link rel="stylesheet" type="text/css" href="styles.css">
22     -->
23 
24   </head>
25   
26   <body>
27   
28     username: <s:property value="username"/><br>
29     name: <s:property value="fileFileName" /><br>
30     fileContentType: <s:property value="fileContentType"/><br>
31     
32           
33   </body>
34 </html>

3、struts.xml中配置信息

 1 <struts>
 2     <package name="default" extends="struts-default">
 3 <!-- 文件上传下载action -->
 4         <action name="fileUpload" class="com.action.UploadAction">
 5         
 6             <result name="success">/fileUploadResult.jsp</result>
 7         
 8         </action>
 9     </package>
10 
11 </struts>

文件上传界面:

上传信息显示

服务器端目标文件

 

推荐阅读