Struts2——文件上传、下载

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

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值