1、文件上传三种方式:
- 将文件以二进制的形式保存到数据库中 activiti工作流框架
- 将文件存储到专门文件服务器(存放文件用的Linux系统)中
- 直接将文件存储到服务器(tomcat所在服务器)中
2、文件上传的一个实例:
action里的代码如下:
private File file;//虚拟路径
private String fileContentType;//文件类型
private String fileFileName;//文件名
private String serverDir="/upload";//服务器中文件保存的文件夹(不用自己新建)
/**
* 上传
* @author LJ
* @Date 2018年10月15日
* @Time 上午11:53:40
* @return
*/
public String upload() {
String realPath=getRealPath(serverDir+"/"+fileFileName);
try {
//文件上传的关键方法
FileUtils.copyFile(file, new File(realPath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
/**
* 获取Linux目录的真实路径
* @author LJ
* @Date 2018年10月15日
* @Time 上午11:26:53
* @param path:指的是本地路径(相对于工程WebContent所在的路径)
* @return
*/
private String getRealPath(String path) {
return application.getRealPath(path);
}
注:前三个变量的变量名是Struts2里固定的,不能更改,此外,还需要给它们提供set/get方法
jsp页面的代码如下:
<form action="${pageContext.request.contextPath }/sy/uploadAction_upload.action" enctype="multipart/form-data" method="post">
<input type="file" name="file" >
<input type="submit" value="上传" >
</form>
注:enctype="multipart/form-data" method="post"也是固定的,声明表单是特殊的提交方式
3、文件下载的一个实例:
action里的代码如下:
/**
* 下载图片
* @author LJ
* @Date 2018年10月15日
* @Time 下午2:33:36
* @return
*/
public String saveAs() {
String type="image/jpeg";//要下载文件的类型
String name="5.jpg";//要下载文件的文件名
response.setContentType(type);
response.setHeader("Content-Disposition","attachment;filename=" + name);//设置响应头
File servletFile=new File(getRealPath(serverDir + "/" + name));//在服务器中找到该文件
try {
//用Struts自己的方法下载
// FileUtils.copyFile(servletFile, response.getOutputStream());
//用缓存流下载
BufferedInputStream bit=new BufferedInputStream(new FileInputStream(servletFile));
BufferedOutputStream bos=new BufferedOutputStream(response.getOutputStream());
copyByBuffered(bit, bos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 用缓存流下载(提高性能)
* @author LJ
* @Date 2018年10月15日
* @Time 下午10:33:28
* @param in
* @param out
* @throws IOException
*/
private void copyByBuffered(BufferedInputStream in, BufferedOutputStream out) throws IOException {
byte[] bt=new byte[1024];
int len=0;
while((len=in.read(bt))!=-1) {
out.write(bt, 0, len);
}
in.close();
out.close();
}
jsp页面的代码如下:
<s:url action="uploadAction_saveAs" namespace="/sy" var="saveUrl"></s:url>
<s:a href="%{#saveUrl}">保存</s:a>
4、展示上传的文件:
action里的代码如下:
/**
* 打开图片
* @author LJ
* @Date 2018年10月15日
* @Time 下午12:00:03
* @return
*/
public String openAs() {
String type="image/jpeg";//要打开文件的类型
String name="5.jpg";//要打开文件的文件名
response.setContentType(type);//内容类型
response.setHeader("Content-Disposition","filename=" + name);//设置响应头
/*
* 从服务器拿图片到本地
* 源:服务器
* 目的:jsp输出图片
*/
File servletFile=new File(getRealPath(serverDir + "/" + name));
try {
FileUtils.copyFile(servletFile, response.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
注: 文件下载与展示的设置响应头的第二个参数只差一个attachment
jsp页面的代码如下:
<s:url action="uploadAction_openAs" namespace="/sy" var="openUrl"></s:url>
<img alt="img" src='<s:property value="#openUrl" />'>
附:
处理文件名的中文乱码:
String fileName = d.getFileName();
fileName = new String(fileName.getBytes("utf-8"), "iso8859-1");struts2文件上传大小设置:
<!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) 10M=10*1024*1024 -->
<constant name="struts.multipart.maxSize" value="10485760"/>struts2文件上传类型设置(通过配置拦截器):
根据struts2自带的fileupload拦截器中提供的allowedTypes来进行限制
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/png,image/gif,image/jpeg</param>
</interceptor-ref>