Struts 2实现文件的上传下载(解除文件大小限制)
程序员文章站
2024-03-11 21:36:49
...
Struts 2实现文件的上传下载
上传:
下载:
环境
MyEclipse 2014
Tomcat:apache-tomcat-8.0.46
JDK:1.7
Sturts版本:2.1
上传文件
1.上传文件界面index.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<s:form action="myupload" method="post" enctype="multipart/form-data">
<s:file name="upload" label="上传的文件"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
</body>
</html>
显示界面success.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>成功页面</title>
</head>
<body>
恭喜你!上传成功!你上传的图片如下:<br/>
<a href="uploadfile/<s:property value="uploadFileName"/>"><img src=uploadfile/<s:property value="uploadFileName"/>></a>
</body>
</html>
2.文件上传功能UploadAction.java
package org.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
private File upload; //上传文件
//属性upload的get/set方法
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
private String uploadFileName; //上传的文件名
//属性uploadFileName的get/set方法
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String execute() throws Exception {
InputStream is=new FileInputStream(getUpload()); //根据上传的文件得到输入流
String directory = "/uploadfile";
String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);
OutputStream os=new FileOutputStream(targetDirectory + "/" +uploadFileName); //指定输出流地址
byte buffer[]=new byte[1024];
int count=0;
while((count=is.read(buffer))>0){
os.write(buffer,0,count); //把文件写到指定位置的文件中
}
os.close(); //关闭
is.close();
return SUCCESS; //返回
}
}
3.在struts.xml
中配置action
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="myupload" class="org.action.UploadAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
这时候访问http://localhost:8080/Ex5.3/index.jsp已经可以上传比较小的文件(<2M),但是一旦上传较大的文件就会报错
要修改文件大小限制,可以在Struts 2的配置文件struts.properties
中修改struts.multipart.maxSize
常量值,但是我们去找这个文件还是比较麻烦的,所以选择在struts.xml
文件中加入下面这一行代码。
<constant name="struts.multipart.maxSize" value="1073741824" />
后面的数字可以根据自己的需要修改
下载文件
1.把文件复制到开发目录
2.下载界面download.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>文件下载</title>
</head>
<body>
<a href="downloadAction.action">下载链接</a>
</body>
</html>
3.DownloadAction.java
package org.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport{
private String downloadFile; //需要下载的文件路径
public String getDownloadFile() {
return downloadFile;
}
public void setDownloadFile(String downloadFile) {
this.downloadFile = downloadFile;
}
public InputStream getTargetFile(){ //根据上面给出的文件路径,生成它的输入流
return ServletActionContext.getServletContext().getResourceAsStream(downloadFile);
}
public String execute() throws Exception{
return SUCCESS;
}
}
4.在struts.xml
配置action
<action name="downloadAction" class="org.action.DownloadAction">
<!-- 传递参数,指定要下载的文件的路径 -->
<param name="downloadFile">/downloadfile/实验5.docx</param>
<result name="success" type="stream">
<!-- 输入流名称,对应Action类中的getTargetFile()方法 -->
<param name="inputName">targetFile</param>
<!-- 设置下载文件的文件名 -->
<param name="contentDisposition">filename="example.docx"</param>
<!-- 指定下载文件时缓冲区的大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
访问http://localhost:8080/Ex5.3/download.jsp就可以点击链接下载文件了
上一篇: 字符串加密 - 牛客网华为机试中等难度
下一篇: JavaIO的常用方法