JAVA SSH ----Struts上传与下载文件
来源:互联网 发布:中文地址匹配 软件 编辑:程序博客网 时间:2024/05/22 08:24
文件上传
Action类
private File image; private String imageContentType; private String imageFileName; public void setImage(File image) { this.image = image; } public void setImageContentType(String imageContentType) { this.imageContentType = imageContentType; } public void setImageFileName(String imageFileName) { this.imageFileName = imageFileName; } public String upload(){ System.out.println("此时的file是"+image+": \r\n" + imageContentType + " : " + imageFileName); String path = ServletActionContext.getServletContext().getRealPath("/upload"); File destFile = new File(path,imageFileName); try { FileUtils.copyFile(image, destFile); } catch (IOException e) { System.out.println("复制文件出现错误"); return "input"; } return SUCCESS; }
upload.jsp页面
<%@taglib uri="/struts-tags" prefix="s" %> <!-- 上传错误信息显示 --> <s:fielderror></s:fielderror> <form action="{pageContext.request.contextPath}/uploadAction.action" method="post" enctype="multipart/form-data" > 上传一个图片吧<br/> <input type = "file" name="image"><br/> <input type="submit" value="提交按钮"/> </form>
注意:表单的enctype类型应该设置为multipart/form-data,file input标签中的name应该与Action类中的File保持一致
配置文件
修改英文错误信息
1、编写一个fileupload.properties 文件(文件名和路径随意)
2、编辑如下
3、加载编写的资源文件 struts.xml
<constant name="struts.custom.i18n.resources" value="cn.itcast.upload.fileupload"></constant>
name属性值默认在default.properties的165行(默认被注释)
value属性值如果文件在包下包使用.分隔,且不要带文件的后缀。如果有多个资源文件使用,号隔开。
* 文件下载 *
Action类
public class DownloadAction extends ActionSupport{ public String list(){ String path = ServletActionContext.getServletContext().getRealPath("/upload"); File file = new File(path); String[] fileNames = file.list(); ActionContext ac = ActionContext.getContext(); Map<String,Object> request = ac.getContextMap(); request.put("fileNames",fileNames); return "list"; } /* 文件下载 */ //1.获取到文件名 private String fileName; public void setFileName(String fileName){ try { fileName = new String(fileName.getBytes("iso8859-1"),"utf-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } this.fileName = fileName; } //2.获取文件流// private InputStream imageStream; public InputStream getImageStream(){ return ServletActionContext.getServletContext().getResourceAsStream("/upload/"+fileName); } //3.给浏览器文件名 public String getDownFileName(){ //需要进行中文编码 try { fileName=URLEncoder.encode(fileName, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } return fileName; } //down方法 public String down(){ System.out.println("down Action run..."); return "down"; }}
jsp文件
<table align="center" border="1"> <tr> <td>编号</td> <td>文件名</td> <td>操作</td> </tr> <c:forEach var="fileName" items="${fileNames }" varStatus="vs"> <td>${vs.count }</td> <td>${fileName }</td> <td> <c:url var="url" value="down_down.action"> <c:param name="fileName" value="${fileName }"></c:param> </c:url> <a href="${url }">下载</a> </td> </c:forEach> </table>
struts配置文件
<action name = "down_*" class="action.DownloadAction" method="{1}"> <result name = "list" >/list.jsp</result> <result name="down" type="stream"> <!-- 允许下载的文件的类型:指定为二进制文件的类型 --> <param name="contentType">application/octet-stream</param> <!-- 对应Action的属性,返回流的属性,其实就是getImageStream --> <param name="inputName">imageStream</param> <!-- 下载头,包括:浏览器显示的文件名 --> <param name="contentDisposition">attachment;filename=${downFileName}</param> <!-- 缓冲区的大小 --> <param name="bufferSize">1024</param> </result> </action>
0 0
- JAVA SSH ----Struts上传与下载文件
- Struts文件上传与下载
- struts文件上传与下载
- java框架Struts学习--文件上传与下载
- struts 实现文件上传与下载
- struts 实现文件上传与下载
- struts 实现文件上传与下载
- struts 实现文件上传与下载
- struts 实现文件上传与下载
- struts 实现文件上传与下载
- struts 1.2 文件上传与下载
- struts 实现文件上传与下载
- Struts实现文件上传与下载
- 文件上传与下载—>struts
- Struts基础学习-文件上传与下载
- 基于struts的文件上传与下载
- struts 学习总结 ---- 文件上传与下载
- struts文件上传与下载简单DEMO
- Canvas实现放大镜效果
- 建立堆复杂度On,均摊思想
- UVA 673 栈的应用
- 读《在你身边,为你设计》
- iOS打包上传AppStore最详细的教程(新手必看)
- JAVA SSH ----Struts上传与下载文件
- hex文件和bin文件区别
- Leetcode-354. Russian Doll Envelopes
- CornerStone 常见的图标含义
- KinectManager 部分笔记
- git+jenkins自动构建一
- Windows下WAMP环境的多站点配置
- 第三天 Java中变量的使用规则及运算符介绍
- Java工程师成神之路