1. 百度了很多 还是 不得要领,于是自己 总结出来
首先上传: jsp页面 < form enctype= multipart/form-data>
<input type="file" name="file1">
然后 action中:
private File file1;
// 文件名
private String file1FileName;
// 文件的类型(MIME)
private String file1ContentType;
private String savePath;
private String uploadName;
// 文件名
private String file1FileName;
// 文件的类型(MIME)
private String file1ContentType;
private String savePath;
private String uploadName;
方法里面就是{ 如下 }
String path = ServletActionContext.getServletContext().getRealPath(savePath);
// 创建存储的目标文件对象
File destFile = new File(path,file1FileName);
System.out.println(",.,.打印.<><<><"+path);
// 把上传的文件,拷贝到目标文件中
FileUtils.copyFile(file1, destFile);
// 创建存储的目标文件对象
File destFile = new File(path,file1FileName);
System.out.println(",.,.打印.<><<><"+path);
// 把上传的文件,拷贝到目标文件中
FileUtils.copyFile(file1, destFile);
--------------------------------------------------
最重要的 就是 xml的配置了,struts自带的 拦截器
<param name="savePath">/uploads</param>
<!-- 限制运行上传的文件的类型 -->
<interceptor-ref name="defaultStack">
<!-- 限制运行的文件的扩展名 -->
<param name="fileUpload.allowedExtensions">txt,jpg,jar</param>
</interceptor-ref>
<!-- 限制运行上传的文件的类型 -->
<interceptor-ref name="defaultStack">
<!-- 限制运行的文件的扩展名 -->
<param name="fileUpload.allowedExtensions">txt,jpg,jar</param>
</interceptor-ref>
————————————————————————
2. 文件的下载 就会比较麻烦
jsp 页面
<c:forEach var="fileName" items="${fileNames}" varStatus="vs">
<tr>
<td>${vs.count }</td>
<td>${fileName }</td>
<td>
<!-- 构建一个url -->
<c:url var="url" value="down_down">
<c:param name="fileName" value="${fileName}"></c:param>
</c:url>
<a href="${url }">下载</a>
</td>
</tr>
</c:forEach>
<tr>
<td>${vs.count }</td>
<td>${fileName }</td>
<td>
<!-- 构建一个url -->
<c:url var="url" value="down_down">
<c:param name="fileName" value="${fileName}"></c:param>
</c:url>
<a href="${url }">下载</a>
</td>
</tr>
</c:forEach>
然后action 页面java代码
/*************2. 文件下载*********************/
public class DownAction extends ActionSupport {
// 1. 获取要下载的文件的文件名
private String fileName;
public void setFileName(String fileName) {
// 处理传入的参数中问题(get提交)
try {
fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
// 把处理好的文件名,赋值
this.fileName = fileName;
}
//2. 下载提交的业务方法 (在struts.xml中配置返回stream)
public String down() throws Exception {
return "download";
}
// 3. 返回文件流的方法
public InputStream getAttrInputStream(){
return ServletActionContext.getServletContext().getResourceAsStream("/upload/" + fileName);
}
// 4. 下载显示的文件名(浏览器显示的文件名)
public String getDownFileName() {
// 需要进行中文编码
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return fileName;
}
}action中的 全部 都是 和 xml 配置 对应的 十分复杂
<result name="downs" type="stream">
<!-- 运行下载的文件的类型:指定为所有的二进制文件类型 -->
<param name="contentType">application/octet-stream</param>
<!-- 对应的是Action中属性: 返回流的属性【其实就是getAttrInputStream()】 -->
<param name="inputName">attrInputStream</param>
<!-- 下载头,包括:浏览器显示的文件名 是 struts 的取值符合 在action里面 取值-->
<param name="contentDisposition">attachment;filename=${downFileName}</param>
<!-- 缓冲区大小设置 -->
<param name="bufferSize">1024</param>
</result>
————————————————————————————
本文介绍如何使用Struts2框架实现文件的上传和下载功能。详细解释了上传过程中的表单设置、Action配置及XML拦截器配置,并演示了下载功能的实现,包括文件名的处理和流的返回。
981

被折叠的 条评论
为什么被折叠?



