Struts2 多文件上传和下载
Download.java:
package com.cjg.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Download extends ActionSupport{
public InputStream getDownloadFile(){
return ServletActionContext.getServletContext ().getResourceAsStream( "/upload/.....txt" );
}
@Override
public String execute() throws Exception
{
return SUCCESS ;
}
}
UploadAction.java
package com.cjg.action;
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 UploadAction extends ActionSupport{
private String username ;
private String password ;
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 String getPassword() {
return password ;
}
public void setPassword(String password) {
this . password = password;
}
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){
if ( file == null ){
super .addFieldError( "file:" , "file not is null!" );
return ERROR ;
}
InputStream is = new FileInputStream ( file .get(i));
String root = ServletActionContext.getRequest ().getRealPath( "/upload" );
System. out .println(root+ "-----------" );
File destFile = new File(root, this .getFileFileName().get(i));
OutputStream os = new FileOutputStream (destFile);
byte [] buffer = new byte [400];
int length = 0;
while ((length = is.read (buffer)) > 0){
os.write (buffer, 0, length);
}
os.close ();
is.close ();
}
return SUCCESS ;
}
}
Message.properties:
struts.messages.error.content.type.not.allowed= /u4e0a/u4f20/u6587/u4ef6/u7c7b/u578b/u4e0d/u5141/u8bb8/uff0c/u8bf7/u91cd/u8bd5/uff01
struts.messages.error.file.too.large= /u4e0a/u4f20/u6587/u4ef6/u8fc7/u5927/uff0c/u8bf7/u91cd/u8bd5/uff01
struts.properties:
struts.i18n.encoding = gbk
struts.xml
<? 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 >
< constant name = "struts.custom.i18n.resources" value = "message" ></ constant >
< constant name = "struts.multipart.saveDir" value = "e:/" ></ constant >
< package name = "struts2" extends = "jfreechart-default" >
< action name = "upload" class = "com.cjg.action.UploadAction" >
< result name = "success" > /upload/uploadResult.jsp </ result >
< result name = "input" > /upload/upload.jsp </ result >
< result name = "error" > /upload/upload.jsp </ result >
< interceptor-ref name = "fileUpload" >
< param name = "maximumSize" > 409600 </ param >
< param name = "allowedTypes" >
image/bmp,image/jpg,image/x-png,image/gif,image/jpeg,text/plain,application/msword,application/excel,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/zip,application/x-zip-compressed,application/rar,application/octet-stream,application/xml,text/xml
</ param >
</ interceptor-ref >
< interceptor-ref name = "defaultStack" ></ interceptor-ref >
</ action >
< action name = "download"
class = "com.cjg.action.Download" >
< result name = "success" type = "stream" >
< param name = "contentType" >
image/bmp,image/jpg,image/x-png,image/gif,image/jpeg,text/plain,application/msword,application/excel,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/zip,application/x-zip-compressed,application/rar,application/octet-stream,application/xml,text/xml
</ param >
< param name = "contentDisposition" >
filename="…...txt"
</ param >
< param name = "inputName" > downloadFile </ param >
</ result >
</ action >
Upload.jsp:
<%@ page language = "java" import = "java.util.*" pageEncoding = "GBK" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< base href = "<%= basePath %> ">
< title > My JSP 'uploadsingle.jsp' starting page </ title >
< meta http-equiv = "pragma" content = "no-cache" >
< meta http-equiv = "cache-control" content = "no-cache" >
< meta http-equiv = "expires" content = "0" >
< meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" >
< meta http-equiv = "description" content = "This is my page" >
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
< script type = "text/javascript" >
function addMore() {
var td = document.getElementById( "more" );
var br = document.createElement( "br" );
var input = document.createElement( "input" );
var button = document.createElement( "input" );
input.type = "file" ;
input.name = "file" ;
button.type = "button" ;
button.value = "Remove" ;
button.οnclick= function () {
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</ script >
</ head >
< body >
< table align = "center" width = "50%" >
< tr >
< td >
< s:fielderror cssStyle = "color:red" />
</ td >
</ tr >
</ table >
< s:form action = "upload" theme = "simple" enctype = "multipart/form-data" method = "post" >
< table align = "center" width = "50%" border = "1" >
< tr >
< td >
username
</ td >
< td >
< s:textfield name = "username" ></ s:textfield >
</ td >
</ tr >
< tr >
< td >
password
</ td >
< td >
< s:password name = "password" ></ s:password >
</ td >
</ tr >
< tr >
< td >
file
</ td >
< td id = "more" >
< s:file name = "file" ></ s:file >< input type = "button" value = "Add More.." onclick = "addMore()" >
</ td >
</ tr >
< tr >
< td >
< s:submit value = " submit " ></ s:submit >
</ td >
< td >
< s:reset value = " reset " ></ s:reset >
</ td >
</ tr >
</ table >
</ s:form >
</ body >
</ html >
Web.xml:
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app version = "2.4"
xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< filter >
< filter-name > struts2 </ filter-name >
< filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
</ filter >
< filter-mapping >
< filter-name > struts2 </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >
< servlet >
< servlet-name > UploadServlet </ servlet-name >
< servlet-class > com.test.servlet.UploadServlet </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > UploadServlet </ servlet-name >
< url-pattern > /UploadServlet </ url-pattern >
</ servlet-mapping >
< welcome-file-list >
< welcome-file > index.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
注意: 1 ,单文件上传只要将其中的属性中的 list 去掉。
2 , allowedType 可以参见 tomcat 中的 web.xml 中的设置。