引言
还记得学习struts1的时候做了文件上传的Demo,现在学习Struts2了,同样,再做一个文件上传的Demo……一、单个文件上传
步骤
先将文件上传相关的jar包引进来,分别是:commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar1、 建立上传文件的index.jsp页面
<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
标题:<input type="text" name="title"><br>
文件:<input type="file" name="myFile"><br>
<input type="submit" value="上传">
</form>
</body>
</html></span>
2、 建立上传成功的success.jsp页面
<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
title:${title }<br>
fileName:${myFileFileName }<br>
myFileContentType:${myFileContentType }
</body>
</html></span>
3、 建立UploadTestAction
<span style="font-size:18px;">package com.bjpowernode.struts2;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import com.opensymphony.xwork2.Action;
public class UploadTestAction {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
//可以得到上传文件的名称
//规则:输入域的名称+固定字符串FileName
private String myFileFileName;
//取得文件数据
//规则:File 输入域的名称
private File myFile;
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
//取得内容类型
//规则:输入域的名称+固定字符串ContentType
private String myFileContentType;
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public String execute() throws Exception{
InputStream is= null;
OutputStream os = null;
try{
is = new BufferedInputStream(new FileInputStream(myFile));
os = new BufferedOutputStream(new FileOutputStream("c:\\" + myFileFileName));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) >0 )
{
os.write(buffer,0,len);
}
}finally{
if(is != null) {is.close();}
if(os!=null){os.close();}
}
return Action.SUCCESS;
}
}
</span>
4、 配置struts.xml配置文件
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 提供更友好的提示信息 -->
<constant name="struts.devMode" value="true"/>
<!-- 需要继承struts-default包,这样就拥有了最基本的功能 -->
<package name="upload-package" extends="struts-default">
<action name="upload" class="com.bjpowernode.struts2.UploadTestAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
</span>
问题
一、
配置一个临时目录
如果有struts2.properties文件可以设置:
<span style="font-size:18px;"> struts.multipart.saveDir =/tmp</span>
<span style="font-size:18px;"> <constant name="struts.multipart.saveDir"value="/tmp"/></span>
文件上传是先将文件传到我们指定的一个临时目录下,然后再通过IO操作将文件写到我们真正要传的目录下。我们在Action中所定义的File类型的成员变量file实际上指向的就是临时目录中的临时文件。
二、
出现了乱码。
我们需要在struts.xml配置文件中进行编码的配置,这里需要注意的是:同时要把jsp页面的编码进行修改,否则不起作用。
<span style="font-size:18px;"><!-- 防止乱码 注意:同时还要把jsp中的编码改成utf-8 -->
<constant name="struts.i18n.encoding" value="utf-8"/></span>
二、多个文件上传
多个文件上传与单个文件上传相似,不过就是需要遍历一下文件而已……步骤
1、 建立上传文件的fileupload2.jsp页面
<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="upload2.action" method="post" enctype="multipart/form-data">
标题:<input type="text" name="title"><br>
文件1:<input type="file" name="file"><br>
文件2:<input type="file" name="file"><br>
文件3:<input type="file" name="file"><br>
<input type="submit" value="上传">
</form>
</body>
</html></span>
2、建立上传成功的success2.jsp页面
<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<!-- username:<s:property value="username"/><br> -->
<s:iterator value="fileFileName" id="f">
file:<s:property value="#f"/><br>
</s:iterator>
</body>
</html></span>
3、建立UploadAction2
<span style="font-size:18px;">package com.bjpowernode.struts2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction2 extends ActionSupport {
private String username;
private List<File> file;
private List<String>fileFileName;
private List<String>fileContentType;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
public String execute() throws Exception{
for(int i=0;i<file.size();i++)
{
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getRequest().getRealPath("/upload");
File destFile = new File(root,fileFileName.get(i));
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length =0;
while(-1!=(length= is.read(buffer)))
{
os.write(buffer,0,length);
}
is.close();
os.close();
}
return SUCCESS;
}
}
</span>
4、配置struts.xml文件
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 防止乱码 注意:同时还要把jsp中的编码改成utf-8 -->
<constant name="struts.i18n.encoding" value="utf-8"/>
<!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 提供更友好的提示信息 -->
<constant name="struts.devMode" value="true"/>
<!-- 配置临时目录 -->
<constant name="struts.multipart.saveDir" value="/tmp"/>
<!--设置文件大小限制 ,也可以在struts.properties文件中进行配置,如果两个都进行了配置,
那么struts.properties的优先级高于struts.xml-->
<constant name="struts.multipart.maxSize" value="10485760"/>
<!-- 需要继承struts-default包,这样就拥有了最基本的功能 -->
<package name="upload-package" extends="struts-default">
<action name="upload" class="com.bjpowernode.struts2.UploadTestAction">
<result>/success.jsp</result>
</action>
<action name="upload2" class="com.bjpowernode.struts2.UploadAction2">
<result>/success2.jsp</result>
<interceptor-ref name="fileUpload">
<!--10485760=1024*1024*10 -->
<param name="maximumSize">10485760</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
</span>
问题
一、
这个问题是因为我没有在tomcat/webapps/struts_13目录下面建立upload文件夹导致的。
上传成功了!
上传成功了!
换个大一点的文件试试
二、
文件超过了限制,所以需要在配置文件中进行大小的限制。
在struts.xml文件中配置:
在struts.xml文件中配置:
<span style="font-size:18px;"><action name="upload2" class="com.bjpowernode.struts2.UploadAction2">
<result>/success2.jsp</result>
<interceptor-ref name="fileUpload">
<!--10485760=1024*1024*10(10M) -->
<param name="maximumSize">10485760</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action></span>
注:当使用struts提供的文件上传拦截器的时候,必须显式的调用默认的拦截器栈。
或者在struts.properties文件中配置:
或者在struts.properties文件中配置:
<span style="font-size:18px;">struts.multipart.maxSize=10485760 </span>
以上就大功告成了!