您的位置:首页 > 编程语言 > Java开发

Struts2文件上传和下载

2016-10-09 13:43 281 查看
文件下载中文乱码未能解救。如有好的方法欢迎指教。

1.单个文件上传和多个文件上传

FileUploadAction.java

package com.lx.utils;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

public class FileUploadAction {
/* private File file;//文件
private String fileFileName;//文件名必须是文件+FileName
private String fileContextType;//file的MIME类型也必须文件+ContextType
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContextType() {
return fileContextType;
}
public void setFileContextType(String fileContextType) {
this.fileContextType = fileContextType;
}
public String execute() throws IOException{

if (file!=null) {
String path = ServletActionContext.getServletContext().getRealPath("/images");
File destFile = new File(path, fileFileName);
//由服务器本地的临时目录,copy的自己指定的目录,即上面指定的images下的目录
FileUtils.copyFile(file, destFile);
System.out.println();

return "success";
}
return "fail";
}
*/

private File[] files;
private String[] filesFileName;
public File[] getFiles() {
return files;
}
public void setFiles(File[] files) {
this.files = files;
}
public String[] getFilesFileName() {
return filesFileName;
}
public void setFilesFileName(String[] filesFileName) {
this.filesFileName = filesFileName;
}
public String execute() throws IOException{

if (files!=null) {
for(int i=0;i<files.length;i++){

String path = ServletActionContext.getServletContext().getRealPath("/images");
File destFile = new File(path, filesFileName[i]);
//由服务器本地的临时目录,copy的自己指定的目录,即上面指定的images下的目录
FileUtils.copyFile(files[i], destFile);
System.out.println();

return "success";
}
}
return "fail";
}
}
2.文件下载
DownloadAction

package com.lx.shopping.utils;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
//服务器端将文件读取供给客户端下载
public final static String path="/file/";
private InputStream inputStream;
private String fileName;

public InputStream getInputStream() {
return inputStream;
}

public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}
public String execute() {
fileName="来.txt";
inputStream=ServletActionContext.getServletContext().getResourceAsStream(path+fileName);
byte[] bytes;
try {
long a=inputStream.available();
System.out.println(a);
bytes = fileName.getBytes("UTF-8");
fileName=new String(bytes,"ISO-8859-1");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;

}
}


3.访问页面
index.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.lbf.test.*"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 其中enctype属性一定要设置为multipart/form-data -->
<form action="file/upload" method="post" enctype="multipart/form-data">
file: <input type="file" name="file"><br>
file: <input type="file" name="file"><br>
file: <input type="file" name="file"><br>
<input type="submit" value="上传">
</form>
<a href="file/download">图片</a>

</body>
</html>4struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" /> -->
<!-- <constant name="struts.devMode" value="true"/> -->
<!-- 设置文件上传的大小,可以在struts2-core-2.5.1.jar下的org/apache/struts2/default.properties下查看默认大小为2M
这个大小为上传的所有文件的总大小 -->
<constant name="struts.multipart.maxSize" value="209715200"></constant>
<package name="upload" extends="struts-default" namespace="/file">
<action name="upload" class="com.lx.shopping.utils.FileUploadAction" >
<result>/success.jsp</result>
<result name="fail">/fail.jsp</result>
<interceptor-ref name="defaultStack">
<!-- fileUpload.maximumSize为上传单个文件大小,所以struts.multipart.maxSize必须>=fileUpload.maximumSize -->
<param name="fileUpload.maximumSize">2097152</param>
<!-- 设置可以上传文件类型 -->
<param name="fileUpload.allowedExtensions">txt,jar,zip</param>
</interceptor-ref>
</action>
</package>

<package name="download" extends="struts-default" namespace="/file" >
<action name="download" class="com.lx.shopping.utils.DownloadAction" >
<result type="stream" >
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">4096</param>
</result>
</action>
</package>
</struts>


5.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>Struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: