Struts2中文件上传下载实例

1.单文件上传

 1 jsp页面:
 2 
 3 <!-- 单文件上传 -->
 4         <form action="Fileupload.action" method="post"
 5             enctype="multipart/form-data">
 6             username:
 7             <input type="text" name="username" />
 8             <br />
 9             file:
10             <input type="file" name="file" />
11             <br />
12             <input type="submit" name="提交" />
13         </form>
14 <!--注意:一定要写enctype="multipart/form-data"-->
 1 action页面:
 2 
 3 package com.Struts2.action;
 4 
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.FileOutputStream;
 8 import java.io.InputStream;
 9 import java.io.OutputStream;
10 
11 import org.apache.struts2.ServletActionContext;
12 
13 import com.opensymphony.xwork2.ActionSupport;
14 
15 public class Fileupload extends ActionSupport {
16 
17     /**
18      * 
19      */
20     private static final long serialVersionUID = 4038542394937191173L;
21 
22     private String username;
23     private File file;
24     private String fileFileName;// 提交过来的file的名字,必须为FileName后缀
25     private String fileContentType;// 提交过来的file的MIME类型,必须以ContentType为后缀
26 
27     public String getUsername() {
28         return username;
29     }
30 
31     public void setUsername(String username) {
32         this.username = username;
33     }
34 
35     public File getFile() {
36         return file;
37     }
38 
39     public void setFile(File file) {
40         this.file = file;
41     }
42 
43     public String getFileFileName() {
44         return fileFileName;
45     }
46 
47     public void setFileFileName(String fileFileName) {
48         this.fileFileName = fileFileName;
49     }
50 
51     public String getFileContentType() {
52         return fileContentType;
53     }
54 
55     public void setFileContentType(String fileContentType) {
56         this.fileContentType = fileContentType;
57     }
58 
59     @Override
60     public String execute() throws Exception {
61         String root = ServletActionContext.getServletContext().getRealPath(
62                 "/upload");
63         InputStream is = new FileInputStream(file);
64         OutputStream os = new FileOutputStream(new File(root, fileFileName));
65 
66         System.out.println("fileFileName:" + fileFileName);
67         // 因为file是存放在临时文件夹的文件,我们可以将其文件名和文件路径打印出来,看和之前的fileFileName是否相同
68         System.out.println("file:" + file.getName());
69         System.out.println("file:" + file.getPath());
70 
71         byte[] buffer = new byte[500];// 用缓冲
72         int length = 0;
73         while (-1 != (length = is.read(buffer, 0, buffer.length))) {
74             os.write(buffer);
75         }
76         os.close();
77         is.close();
78 
79         return SUCCESS;
80     }
81 }

 

2.多文件上传

 1 jsp页面:
 2 
 3 <script type="text/javascript">
 4             $(function() {
 5             $("#button").click(function (){
 6                 var html = $("<input type='file' name='file'/><br/>");
 7                 var button = $("<input type='button' name='button' value='删除'/><br/>");
 8                 $("#body div").append(html).append(button);
 9                     button.click(function (){
10                         html.remove();
11                         button.remove();            
12                         });
13             });
14         });
15         </script>
16 <body id="body">
17 <!-- 多文件上传 -->
18         <form action="manyFileupload.action" method="post"
19             enctype="multipart/form-data">
20             username:
21             <input type="text" name="username" />
22             <br />
23             file:
24             <input type="file" name="file" />
25             <br />
26             <input type="button" value="添加" id="button" />
27             <br />
28             <div></div>
29             <input type="submit" value="提交" />
30         </form>
31     </body>
action页面:package com.Struts2.action;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/*** 多文件上传* * @author admin* */
public class ManyFileupload extends ActionSupport {/*** */private static final long serialVersionUID = -1060365794705605882L;private String username;private List<File> file;// 因为有多个文件,所以用list集合,file属性指上传到的临时空间文件夹(upload)中的绝对路径private List<String> fileFileName;// 上传的文件名称,自动会加载,因为是以FileName为后缀,会自动识别文件名称private List<String> fileContentType;// 上传过来的文件类型,以ContentType后缀,自动识别类型(如:图片类型就是image/jepg)public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public List<File> getFile() {return file;}public void setFile(List<File> file) {this.file = file;}public List<String> getFileFileName() {return fileFileName;}public void setFileFileName(List<String> fileFileName) {this.fileFileName = fileFileName;}public List<String> getFileContentType() {return fileContentType;}public void setFileContentType(List<String> fileContentType) {this.fileContentType = fileContentType;}@Overridepublic String execute() throws Exception {String root = ServletActionContext.getServletContext().getRealPath("/upload");for (int i = 0; i < file.size(); i++) {InputStream is = new FileInputStream(file.get(i));OutputStream os = new FileOutputStream(new File(root, fileFileName.get(i)));byte[] buffer = new byte[500];int length = 0;while (-1 != (length = is.read(buffer, 0, buffer.length))) {os.write(buffer);}os.close();is.close();}return super.execute();}}

3.文件下载

1 jsp页面
2 
3         <!-- 下载 -->
4         <form action="fileDownload.action">
5             <input type="submit" value="下载" />
6         </form>
7     
action页面package com.Struts2.action;import java.io.InputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/*** 文件下载* * @author admin* */
public class FileDownload extends ActionSupport {/*** */private static final long serialVersionUID = 509550154000070698L;public InputStream getDownloadFile() {return ServletActionContext.getServletContext().getResourceAsStream("upload/psb.jpg");// 从upload中选择一张图片进行下载
    }@Overridepublic String execute() throws Exception {return super.execute();}
}

4.struts.xml

 1     <!-- 定义全局结果:必须写在action上面 -->    
 2     <global-results>
 3         <result name="success">/success.jsp</result>
 4     </global-results>
 5     
 6     <!-- 单文件上传 -->
 7     <action name="Fileupload" class="com.Struts2.action.Fileupload"></action>
 8     <!-- 多文件上传-->
 9     <action name="manyFileupload" class="com.Struts2.action.ManyFileupload"></action>
10     <!-- 文件下载 -->
11     <action name="fileDownload" class="com.Struts2.action.FileDownload">
12         <result name="success" type="stream">
13              <param name="contentDisposition">attachment;filename="psb.jpg"</param><!-- attachment表示弹出下载提示框 -->
14              <param name="inputName">downloadFile</param><!-- downloadFile对应action中的getDownloadFile方法 -->
15         </result>
16     </action>    

 

 

本博客源码出自:http://www.cnblogs.com/xiaoluo501395377/archive/2012/10/26/2740882.html。谢谢他的分享!

 

转载于:https://www.cnblogs.com/lirenzhujiu/p/5749303.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/256642.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

最优化课堂笔记06-无约束多维非线性规划方法(续)

6.5共轭方向法 6.5.1 共轭方向 6.5.1 共轭梯度法 6.6单纯形法(不考) 6.7最小二乘法 6.7.2 改进的高斯-牛顿最小二乘法

opengl微发展理解

1.什么是OpenGL? 一种程序&#xff0c;可以与界面和图形硬件交互作用、一个开放的标准 2.软件管道 请看上图 - Apllication层 表示你的程序&#xff08;调用渲染命令。如opengl API&#xff09; -Abstraction层 表示画图接口&#xff08;如OpenGL API或者DirectX API&a…

MacosX 下GCC编译指定版本的代码

export MACOSX_DEPLOYMENT_TARGET10.6转载于:https://www.cnblogs.com/lovelylife/p/5754226.html

最优化作业第六章——共轭梯度法和鲍尔法

共轭梯度法&#xff1a; 代码&#xff1a; #导入模块 from sympy import * import sympy as sp #将导入的模块重新定义一个名字以便后续的程序进行使用 from numpy import * import numpy as npdef main():#本例是利用共轭梯度法进行最优化x1,x2,alpha symbols("x1,x2,…

酒鬼随机漫步(一个矢量类)

摘要: 阅读全文这是一个定义的一个矢量类&#xff0c; 然后用矢量类模拟一个酒鬼的随机漫步 问题很简单&#xff0c; 实现也不麻烦&#xff0c; 但是这个小程序却可以呈现出许多语法知识。而且代码风格也不错&#xff0c;因此保存在了这篇博客中。 建议&#xff1a; 1. 类的声…

对高并发流量控制的一点思考

前言 在实际项目中&#xff0c;曾经遭遇过线上5WQPS的峰值&#xff0c;也在压测状态下经历过10WQPS的大流量请求&#xff0c;本篇博客的话题主要就是自己对高并发流量控制的一点思考。 应对大流量的一些思路 首先&#xff0c;我们来说一下什么是大流量&#xff1f; 大流量&…

ndk学习19: 使用Eclipse调试so

1. 设置调试选项在AndroidManifest文件加入允许调试android:debuggable"true" 此时编译项目会多出:2. 配置调试代码把需要调试的代码,放如按钮事件中,如果放在OnCreate会导致连接调试器时,代码已经跑完了Button btnTest (Button)findViewById(R.id.button1);btnT…

Inside the C++ Object Model | Outline

《Inside the C Object Model&#xff08;C对象模型&#xff09;》&#xff0c;这是一本灰常不错的书&#xff01; CSDN下载页面&#xff08;中文&#xff0c;侯捷译&#xff09; 豆瓣评论 读书笔记目录如下&#xff08;不定时更新&#xff09;&#xff1a; 转载于:https://www…

最优化课程笔记07——约束问题的非线性规划方法(重点:拉格朗日乘子法和惩罚函数法)

7.1 间接法&#xff1a;约束转化为无约束问题&#xff08;含一个重点&#xff1a;拉格朗日乘子法&#xff09; 当维数多的时候不适用 7.1.2拉格朗日乘子法&#xff08;重点&#xff09; 7.1.2.1 等式约束问题 7.1.2.2 不等式约束问题 7.1.3 惩罚函数法&#xff08;内惩罚函数法…

工业相机:传感器尺寸与像元尺寸的关系

相同分辨率的工业相机&#xff0c;传感器面积越大&#xff0c;则其单位像素的面积也越大&#xff0c;成像质量也会越好。同样的500万像素的工业相机&#xff0c;2/3”的传感器成像质量就要优于1/2”的。一般来说&#xff0c;工业相机的靶面大小&#xff0c;如果要求不是太严格&…

macOS下安装ipython

macOS下sudo安装ipython&#xff0c;会提示限错误&#xff1a; [Errno 1] Operation not permitted: /tmp/pip-Elrhse-uninstall/System/Library... 解决方法&#xff1a; pip install ipython --user -U 参考&#xff1a; http://chaishiwei.com/blog/994.html 本文转自 h2app…

结构化查询语言包含哪些方面?

结构化查询语言SQL&#xff08;STRUCTURED QUERY LANGUAGE&#xff09;是最重要的关系数据库操作语言&#xff0c;并且它的影响已经超出数据库领域&#xff0c;得到其他领域的重视和采用&#xff0c;如人工智能领域的数据检索&#xff0c;第四代软件开发工具中嵌入SQL的语言等。…

Opencv 找轮廓并画出相应的矩形

找轮廓参考以下大神的&#xff0c;对于里面的方法和结果存储解释的很清楚&#xff1b; http://blog.csdn.net/gubenpeiyuan/article/details/44922413 缺少的是画相应包围矩形的&#xff0c;其中找矩形用最小外接矩形函数cvMinAreaRect2 。 CvBox2D rect; CvPoint2D32f Corner…

C# 图片识别(支持21种语言)

图片识别的技术到几天已经很成熟了&#xff0c;只是相关的资料很少&#xff0c;为了方便在此汇总一下&#xff08;C#实现&#xff09;&#xff0c;方便需要的朋友查阅&#xff0c;也给自己做个记号。 图片识别的用途&#xff1a;很多人用它去破解网站的验证码&#xff0c;用于达…

搭建Git Server - Centos+Gitosis

参考并部分转载自&#xff1a;http://www.pfeng.org/archives/757 1. 安装依赖 yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel git python python-setuptools2. 安装gitosis git clone git://github.com/res0nat0r/gitosis.git cd…

php中rsa加密及解密和签名及验签

加密的内容长度限制为密钥长度少11位,如128位的密钥最多加密的内容为117个长度。 公钥加密    $public_contentfile_get_contents(公钥路径);    $public_keyopenssl_get_publickey($public_content);        $original_str待加密的内容;    $original_arr…

Opencv ---像素坐标转世界坐标(已知外参)

只能求取已知外参的世界坐标平面上的世界坐标&#xff0c;具体公式如图片所示&#xff01; PS&#xff1a;字丑请谅解&#xff01;

最优化5-8章重点(考试点全)

10道题&#xff0c;每道题10分&#xff0c;5-8章大概4题左右&#xff0c;后面的章节主要考的是概念题

多对多关联映射(双向)

关联映射方面的最后一篇了&#xff0c;我觉得映射文件的编写是使用hibernate的基础&#xff0c;而关联映射又是基础的基础&#xff0c;所以这方面分的细一些&#xff0c;罗嗦一些&#xff0c;说明白就好&#xff0c;呵呵。多对多关联(双向)&#xff0c;相对单向&#xff0c;在实…