文件上传
UploadAction.java
package com.wansha.struts.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction 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;
}
@Override
@SuppressWarnings("deprecation")
public String execute() throws Exception {
for(int i=0; i<file.size(); i++){
String path = ServletActionContext.getRequest().getRealPath("/upload");
System.out.println("filepath-->"+file.get(i).getAbsolutePath());
File directory = new File(path);
if(!directory.exists())directory.mkdirs();
OutputStream os = new FileOutputStream(new File(path,fileFileName.get(i)));
InputStream is = new FileInputStream(file.get(i));
byte[] by = new byte[4096];
int length = 0;
while(-1 != (length = is.read(by,0,by.length))){
os.write(by,0,length);
}
os.close();
is.close();
}
return SUCCESS;
}
}
文件上传页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.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">
</head>
<body>
<s:form action="fileUpload" enctype="multipart/form-data" theme="simple" method="post">
<s:textfield name="username" /><br />
<s:file name="file" /><br />
<s:file name="file" /><br />
<s:file name="file" /><br />
<s:submit value="提交" />
</s:form>
</body>
</html>
文件上传后的结果 页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'uploadresult.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">
-->
</head>
<body>
username:<s:property value="username" />
<s:iterator value="fileFileName" id="f">
<s:property value="#f" />
</s:iterator>
</body>
</html>
DownloadFileAction.java 文件下载
package com.wansha.struts.action;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadFileAction extends ActionSupport {
private String fileName;
private InputStream is;
public String getFileName() throws UnsupportedEncodingException {
return new String(this.fileName.getBytes("UTF-8"),"8859_1");
}
public void setFileName(String fileName) throws UnsupportedEncodingException {
this.fileName = new String(fileName.getBytes("8859_1"),"UTF-8");
}
public InputStream getDownloadFile(){
this.is = ServletActionContext.getServletContext().getResourceAsStream("/upload/"+this.fileName);
return this.is;
}
public boolean isFile(){
File file = new File("/upload/",this.fileName);
return file.exists();
}
@Override
public String execute() throws Exception {
if(isFile()){
return INPUT;
}
return SUCCESS;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.multipart.saveDir" value="/upload"></constant><!-- 临时存储文件夹 -->
<constant name="struts.multipart.maxSize" value="209715200"></constant><!-- 最大上传大小-->
<package name="view" extends="struts-default">
<global-results>
<result name="filenotfound">/filenotfound.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="filenotfound" exception="com.wansha.struts.exception.UnKnowErrorException"></exception-mapping>
</global-exception-mappings>
<action name="fileUpload" class="com.wansha.struts.action.UploadAction">
<result name="success">/uploadresult.jsp</result>
</action>
<action name="download" class="com.wansha.struts.action.DownloadFileAction">
<result type="stream">
<param name="contentDisposition">attachment;filename=${fileName}</param>
<param name="inputName">downloadFile</param>
</result>
<result name="input">/filenotfound.jsp</result>
</action>
</package>
</struts>
downloadFile.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'downloadFile.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">
-->
</head>
<body>
<a href="download?fileName=未命名.jpg">未命名.jpg文件下载</a><br />
<a href="download?fileName=appstore2.sql">appstore2.sql文件下载</a><br />
<a href="download?fileName=数据结构(Java).pdf">数据结构(Java).pdf文件下载</a><br />
<a href="download?fileName=site.xml">site.xml文件下载</a><br />
</body>
</html>
filenotfound.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'filenotfound.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">
-->
</head>
<body>
找不到该文件!
</body>
</html>