struts2的文件上传与下载

package strut2实现文件的上传及下载和相关的jsp代码;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

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

import com.opensymphony.xwork2.ActionSupport;

public class fileUpOrDownAction extends ActionSupport{
/**
* 这是action, jsp代码在下面
*/
private static final long serialVersionUID = 1L;
private File file; //前台表单提交的name属性要与此行相同
private String fileContentType; //以下两个属性名是struts2特有的方法,由struts过滤器提供并支持的
private String fileFileName; //固定写法 ;文件类型 = file+ContentType 文件名 = file+ FileName 此处较为重要

//处理文件上传 , 处理文件类型是非常重要的
public String upload() throws Exception{
//首先要获取保存路径的绝对路径
String savePath = ServletActionContext.getServletContext().getRealPath("upload"); //此处是Webroot目录下的upload文件夹,需提前建立该文件夹,否则会报错
//为避免上传文件的文件名重复,造成文件的覆盖 , 必须将文件的文件名重新命名 , UUID产生随机数的 , 在后面拼接文件的文件类型
String fileName = UUID.randomUUID().toString().replace("-", "")+fileContentType.substring(fileFileName.lastIndexOf("."));
//将获取到文件复制到 硬盘进行持久化保存
FileUtils.copyFile(file, new File(savePath,fileName)); //file是要被复制的文件内容 该行中的new File()第一个参数是保存路径, 第二个参数是复制过来文件的名字

return "upload";
}

//至此,成功将前端上传的文件持久化的保存到 WEBroot/upload 文件夹下
/*Struts2 支持文件上传的功能 但须导入 commons-fileupload-1.2.2.jar commons-io-2.0.1.jar 这两个包与struts2所必须的核心包
*

* struts.xml必须加入 文件限制的相关参数
<action name="*" class="File_about_upload.Test1Action" method="{1}">

<!-- 该部分用于设置上传文件的相关参数 -->
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">500000000</param>
<!-- <param name="fileUpload.allowedTypes">text/plain,application/vnd.ms-powerpoint</param>
<param name="fileUpload.allowedExtensions">.txt,.ppt,.</param> -->
</interceptor-ref>

<result name="upload">/index.jsp</result>


<?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>
<!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
<constant name="struts.action.extension" value="do" />
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<!--<constant name="struts.objectFactory" value="spring" />-->
<!--解决乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.multipart.maxSize" value="10701096"/>

<package name="upload" namespace="/upload" extends="struts-default">
<action name="*_upload" class="com.ljq.action.UploadAction" method="{1}">
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
<package name="upload1" namespace="/upload1" extends="struts-default">
<action name="upload1" class="com.ljq.action.UploadAction2" method="execute">
<!-- 要创建/image文件夹,否则会报找不到文件 -->
<param name="savePath">/image</param>
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
<package name="upload2" namespace="/upload2" extends="struts-default">
<action name="upload2" class="com.ljq.action.UploadAction3" method="execute">
<!-- 要创建/image文件夹,否则会报找不到文件 -->
<param name="savePath">/image</param>
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
*/





private InputStream downloadFile;
private String filePath;
private String fileName;

public String download() throws Exception{
//1. 首先要获取文件的绝对路径或相对路径
File inputfile = new File("你要传的文件具体路径");

//2. 将文件以输入流(InPutStream) 方式 赋值给download
downloadFile = new FileInputStream(inputfile);

return "download";
}
//重点在于struts配置文件上
/*
* <result name="download" type="stream"> //注意, 一定要注明数据类型为stream
<param name="contentDisposition">attachment;filename=test.jpg</param> // attachment 是表示文件在页面上以什么方式打开 fileName = 在页面显示的文件名称
<param name="inputName">downloadFile</param> //inputName 是文件输入流的名称
<param name="bufferSize">1024</param> //文件缓冲流的大小
</result>
*/

/*前台页面,选择图片时预览代码
* <body>
This is my JSP page. <br>
<h3>请选择图片文件:JPG/GIF</h3>
<form name="form0" id="form0">
<input type="file" name="file" id="file" multiple="multiple" /><br>
<img src="" id="img" style=" 20rem;height: 15rem;">
</form>
<script>
$("#file").change(function(){
var objUrl = getObjectURL(this.files[0]) ;//获取文件信息
console.log("objUrl = "+objUrl);
if (objUrl) {
$("#img").attr("src", objUrl);
}
}) ;
function getObjectURL(file) {
var url = null;
if (window.createObjectURL!=undefined) {
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}
</script>
*/


public InputStream getDownloadFile() {
return downloadFile;
}
public void setDownloadFile(InputStream downloadFile) {
this.downloadFile = downloadFile;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
}

//文件下载jsp页面暂不提供

原文地址:https://www.cnblogs.com/ccq-190203/p/10458539.html