关于Struts2 文件上传下载
这几天在学习Struts2,初次接触Struts2,对其甚是好奇,该框架为我们封装了文件的上传下载代码,省去了诸多不必要的麻烦,比如要用代码设置一些文件大小,文件类型的什么的。我们只需要配置下配置文件,写一点点java代码就可以实现上传下载,好方便。
Action怎么实现的就不多说了(extends ActionSupport类等),直接描述功能实现吧!
圈中的看,其他忽略,其他是在学习Struts2的时候的代码
1.上传
a. 书写前台书写form表单
struts2是我的工程名
方法一:
<form action="/struts2/day4/upanddown!upLoad" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
方法二:
由于我在查询下载中文乱码怎么解决的时候,发现该form表单还可以这样写, Struts2框架的标签库为我们封装了该form表单
<!--导入标签库-->
<%@ taglib prefix="s" uri="/struts-tags" %>
<!--form标签-->
<s:form action="/day4/upanddown!upLoad" enctype="multipart/form-data" method="post">
<s:file name="file" label="文件上传"></s:file>
<s:submit value="立即上传"></s:submit>
</s:form>
注意:这里的action路径有点不一样,跳转的话,前面/的话不能加工程名,可能是该标签/就表示当前工程,而JSP的/表示http://localhost:8080/这个路径
b. Aciton中定义一个成员变量,变量名和input file标签中name属性值相同,提供getter/setter。
当上传的时候,Struts2读取该文件,在服务器内存中建立个临时的文件xxx.tmp,(通过打印这个file就可以得知),以供IO流读写
c。输入流输出流
InputStream 自然是根据ServletContext,根据要上传的相对路获取绝对路径,
OuputStream也是。
上传搞定。上传不用注意文件的中文问题,到现在还没遇到过。遇到再在下一篇文章中解决
2.下载
简单下载(没注意中文编码问题,只是提供个简单的怎么上传下载)
a. Action中定义输入流InputStream为成员变量,为其提供getter/setter
private InputStream fileIN;
b.下载方法fileDownLoad中,通过前台页面发来的文件名,获取下载路径,完成这个InputStream的实现
fileIN = new FileInputStream(路径);
c。配置struts.xml配置文件
1)result的type属性设置为Stream,不进行页面跳转
<result name="fileDownLoad" type="stream">
。。。
</result>
2)配置2个参数
<param name="inputName">fileIN</param>
<param name="contentDisposition">attachment;fileName=${file}</param>
第一个参数: 文件输入流 ,对应Action中刚刚定义的InputStream成员变量
第二个参数:配置在浏览器中下载的时候展开方式(直接查看/弹出点击保存或者运行框)
attachment;表示弹出点击保存的框
fileName=${file} :表示默认在浏览器显示的保存的文件名 ${file}是Struts的EL表达式,这里是获取Action中接收到的文件名,它是定义在Aciton的成员变量
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.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.custom.i18n.resources" value="appRes"></constant>
<package name="day4" namespace="/day4" extends="struts-default">
<action name="upanddown" class="com.struts.day4.DownLoad">
<result name="downLoad">/updowns/download.jsp</result>
<result name="upLoad" type="redirectAction">upanddown!downLoad</result>
<result name="fileDownLoad" type="stream">
<param name="contentType">application/octet-stream;charset=UTF-8 </param>
<param name="inputName">fileIN</param>
<param name="contentDisposition">attachment;fileName=${file}</param>
</result>
</action>
</package>
</struts>
java代码:
package com.struts.day4;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownLoad extends ActionSupport{
private String file; //对应<input name="file">和下载的时候前台发来的文件名
private String fileFileName; //上传时获取文件名
private String fileContentType; //上传时获取文件烈性
private InputStream fileIN; //下载的时候的文件输入流
/**
* @return the fileIN
*/
public InputStream getFileIN() {
return fileIN;
}
/**
* @param fileIN the fileIN to set
*/
public void setFileIN(InputStream fileIN) {
this.fileIN = fileIN;
}
/**
* @return the fileContentType
*/
public String getFileContentType() {
return fileContentType;
}
/**
* @param fileContentType the fileContentType to set
*/
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
/**
* @return the fileFileName
*/
public String getFileFileName() {
return fileFileName;
}
/**
* @param fileFileName the fileFileName to set
*/
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
/**
* @return the file
*/
public String getFile() {
return file;
}
/**
* @param file the file to set
*/
public void setFile(String file) {
this.file = file;
}
public String downLoad(){
System.out.println("查询显示列表");
ActionContext context1= ActionContext.getContext();
ServletContext context = ServletActionContext.getServletContext();
String realPath = context.getRealPath("/files");
File f = new File(realPath);
String[] files = f.list();
List<String> fs = Arrays.asList(files);
System.out.println(fs);
context1.put("files", fs);
return "downLoad";
}
/**
* 上传
* @return
*/
public String upLoad(){
System.out.println("上传");
System.out.println(fileContentType);
ServletContext context = ServletActionContext.getServletContext();
String realPath = context.getRealPath("/files");
System.out.println("realPath:"+realPath);
System.out.println(file);
String outPath = realPath+"/" + fileFileName;
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(file);
os = new FileOutputStream(outPath);
byte[] buf = new byte[1024];
int len = -1;
while(true){
len = is.read(buf);
if(len == -1){
break;
}
os.write(buf);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
is.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "upLoad";
}
/**
* 文件下载
* @return
*/
public String fileDownLoad(){
System.out.println("下载");
System.out.println("下载的文件名是"+file);
ServletContext context = ServletActionContext.getServletContext();
String realPath = context.getRealPath("/files");
try {
fileIN = new FileInputStream(realPath +"/"+ file);
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "fileDownLoad";
}
}