1、文件上传
文件上传准备:
注意:
1)表单必须使用POST方式提交(GET方式数据大小不能超过1KB);
2)使用二进制编码.multipart/form-data(把文件的数据发送给服务端而不是文件的名).
3)<input type="file" name=""/>
eg:
①upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<h3>文件上传</h3>
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
上传图片:<input type="file" name="headImg"><br>
<input type="submit" value="提交">
</form>
②UploadAction.java
扫描二维码关注公众号,回复:
1849328 查看本文章
![](/qrcode.jpg)
public class UploadAction extends ActionSupport{
private static final long serialVersionUID = 1L;
//注意:headImg并不是指前端jsp上传过来的文件本身,而是文件上传过来存放在临时文件夹下面的文件
private File headImg;
//提交过来的headImg的名字
private String headImgFileName;
public String upload() throws IOException{
//获取根路径WebContent下upload文件夹的路径
String dir = ServletActionContext.getServletContext().getRealPath("/upload");
//System.out.println(headImg.getName());
//System.out.println(headImg.getPath());
System.out.println(dir);
File dest = new File(dir,headImgFileName);
if(!dest.getParentFile().exists()){
dest.getParentFile().mkdir();
}
FileUtils.copyFile(headImg, dest);
return NONE;
}
public File getHeadImg() {
return headImg;
}
public void setHeadImg(File headImg) {
this.headImg = headImg;
}
public String getHeadImgFileName() {
return headImgFileName;
}
public void setHeadImgFileName(String headImgFileName) {
this.headImgFileName = headImgFileName;
}
}
③struts.xml
<struts>
<package name="uploadPkg" extends = "struts-default" namespace="/">
<action name="upload" class="action.upload.UploadAction" method="upload"/>
</package>
</struts>
④测试
上传图片
控制台打印出存取图片的路径
在计算机查找该路径,找到图片
注意:
此处疑点:为什么路径不是F:\eclipseWrokspace\structTwo2\WebContent\upload而是F:\eclipseWrokspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\structTwo2\upload?
这是在Eclipse中配置完Tomcat后,发布到的部署路径
2、文件下载
eg:
①download.jsp
<h3>文件下载列表</h3>
<a href="${pageContext.request.contextPath}/down/download?fileName=logo.zip">logo</a>
<a href="${pageContext.request.contextPath}/down/download?fileName=图标.zip">图标</a>
②DownloadActon.java
public class DownloadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String fileName;
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileName() throws Exception {
System.out.println(fileName);
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
return fileName;
}
public InputStream getDownloadFile() throws Exception {
String dir = ServletActionContext.getServletContext().getRealPath(
"/WEB-INF/down");
File f = new File(dir, fileName);
System.out.println(f);
return new FileInputStream(f);
}
@Override
public String execute() throws Exception {
System.out.println(fileName);
return "success";
}
}
③struts.xml
<struts>
<package name="downloadPkg" extends="struts-default" namespace="/down">
<action name="download" class="action.download.DownloadAction">
<!-- type一定要定义成stream类型,告诉action这是文件下载的result -->
<result name="success" type="stream">
<!-- contentDisposition是文件下载的处理方式,包括内联(inline)和附件(attachment), 默认是inline,
使用附件时这样配置:attachment;filename="文件名" 。 -->
<param name="contentDisposition">attachment;fileName="${fileName}"</param>
<!-- 由getDownloadFile()方法获得inputStream., inputName这个属性就是得到action中的文件输入流,名字一定要和action中的输入流属性名字相同 -->
<param name="inputName">downloadFile</param>
</result>
</action>
</package>
</struts>
④测试
文件目录结构:
下载页面:
注意:
可以看到在struts.xml中配置result类型为stream,在struts-default中配置为:
查看该类源代码
a.
b.