Struts2中的文件上传与下载

本人小白,已经学到Struts2的部分了,有一些内容分享给大家,如果有错误的话,请指出来,我好修改。

Struts框架中,具备文件上传和下载的功能 ,首先想要上传文件,那么必须先指定上传的表单为文件上传表单

<span style="font-size:18px;"><body>
<form action="${pageContext.request.contextPath}/upload/fileupload" enctype="multipart/form-data" method="post">
	用户名:<input type="text" name="userName" /><br/>
	选择文件:<input type="file" name="file1" /><br/>
	<input type="submit" value="上传">
</form>



</body></span>
上面的enctype类型必须指定为"multipart/form-data",这样上传的时候,Action才会识别其为文件上传表单。否则是提交不上去的。

接下来就是Action中的代码了:

public class FileUploadAction extends ActionSupport {

	private String userName;//用户名
	private File file1;		//文件对象
	private String file1FileName;//文件名
	private String file1ContentType;//文件类型
	public String getUserName() {
		return userName;
	}
	public String getFile1FileName() {
		return file1FileName;
	}
	public void setFile1FileName(String file1FileName) {
		this.file1FileName = file1FileName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public File getFile1() {
		return file1;
	}
	public void setFile1(File file1) {
		this.file1 = file1;
	}
	public String getFile1ContentType() {
		return file1ContentType;
	}
	public void setFile1ContentType(String file1ContentType) {
		this.file1ContentType = file1ContentType;
	}
	@Override
	public String execute() throws Exception {
		
		System.out.println("处理中");
		//获取要输出文件的路径
		String realPath = ServletActionContext.getServletContext().getRealPath("/files");
		//指定这个文件名和路径,但是如果想不被下次上传的文件覆盖,就必须在指定文件名前面加上前缀进行区分
		File file = new File(realPath,this.file1FileName);
		//使用FileUtils进行复制文件
		FileUtils.copyFile(file1, file);//把浏览器传递过来的文件进行复制,内容写到file文件中
		
		return SUCCESS;
	}

}
使用Struts中的FileUtils工具类,来支持文件的复制,下面是struts.xml的配置
public class DownloadAction extends ActionSupport {
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>public String list()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>//显示文件列表名,输出到浏览器
<span style="white-space:pre">		</span>//1.获取存放文件的路径
<span style="white-space:pre">		</span>String realPath = ServletActionContext.getServletContext().getRealPath("/files");
<span style="white-space:pre">		</span>File file = new File(realPath);
<span style="white-space:pre">		</span>//2.得到这个目录下,所有文件名
<span style="white-space:pre">		</span>String[] list = file.list();
<span style="white-space:pre">		</span>//3.放到域里面
<span style="white-space:pre">		</span>Map<String, Object> application = ActionContext.getContext().getApplication();
<span style="white-space:pre">		</span>application.put("list", list);
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>return "list";
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>public String down()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>return "downFile";
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>//表单传递过来的文件名
<span style="white-space:pre">	</span>private String fileName;
<span style="white-space:pre">	</span>public String getFileName() {
<span style="white-space:pre">		</span>return fileName;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 解决表单get方式提交时,传递过来的参数编码问题
<span style="white-space:pre">	</span> * @param fileName
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public void setFileName(String fileName) {
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>fileName = new String(fileName.getBytes("iso8859-1"),"utf-8");
<span style="white-space:pre">		</span>} catch (UnsupportedEncodingException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>this.fileName = fileName;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 得到这个文件输入流,并通过struts转换成输出流输出到浏览器
<span style="white-space:pre">	</span> * @return
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public InputStream getInStream()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>InputStream in = ServletActionContext.getServletContext()
<span style="white-space:pre">				</span>.getResourceAsStream("/files/"+fileName);//指定目录下,指定文件名
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>return in;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 把下载的名字输出到浏览器,在struts.xml中的配置就是downloadfileName,意思为下载名
<span style="white-space:pre">	</span> * @return
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public String getDownLoadFileName()
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>String str = null;
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>String encode = URLEncoder.encode(fileName, "utf-8");
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>str = encode;
<span style="white-space:pre">		</span>} catch (UnsupportedEncodingException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">			</span>throw new RuntimeException(e);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>return str;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>

在Struts.xml的配置如下:

<action name="down_*" class="cn.itcast.f_upload.DownloadAction" method="{1}">
	        <result name="list">/showlist.jsp</result>
	        <!-- 配置下载参数的信息 -->
	        <result name="downFile" type="stream">
	            
	            <param name="contentType">application/octet-stream</param>
			  	
			  	<!-- 对应的action中的返回流的属性名称 -->	
			   <param name="inputName">inStream</param>
			   <!-- action中对应的下载文件名,必须通过URLencode来转换文件名 -->
			   <param name="contentDisposition">attachment;filename=${downLoadFileName}</param>
			   <param name="bufferSize">1024</param>
	            
	        </result>
	        
	    </action>
配置中指定的名称和流还有显示到浏览器的文件名,都必须指定好。
先在浏览器显示文件夹下面所有的文件:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<table>
	<tr>
	<td>编号</td>
	<td>文件名</td>
	<td>下载</td>
	</tr>
	
	<c:forEach var="filelist" varStatus="vs" items="${list }">
		<tr>
		<td>${vs.count}</td>
		<td>${filelist}</td>
		<td>
		<a href="${pageContext.request.contextPath}/upload/down_down?fileName=${filelist}">下载</a>
		</td>
		</tr>
	</c:forEach>
</table>
</body>
</html>

在得到所有显示信息后,便可以下载啦。


这就是Struts中的上传与下载,只要注意到细节,就不会出错的

























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值