Struts2中文件上传下载实例
1.单文件上传
jsp页面: <!-- 单文件上传 -->
<form action="Fileupload.action" method="post"
enctype="multipart/form-data">
username:
<input type="text" name="username" />
<br />
file:
<input type="file" name="file" />
<br />
<input type="submit" name="提交" />
</form>
<!--注意:一定要写enctype="multipart/form-data"-->
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 org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class Fileupload extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 4038542394937191173L; private String username;
private File file;
private String fileFileName;// 提交过来的file的名字,必须为FileName后缀
private String fileContentType;// 提交过来的file的MIME类型,必须以ContentType为后缀 public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public File getFile() {
return file;
} public void setFile(File file) {
this.file = file;
} public String getFileFileName() {
return fileFileName;
} public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
} public String getFileContentType() {
return fileContentType;
} public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
} @Override
public String execute() throws Exception {
String root = ServletActionContext.getServletContext().getRealPath(
"/upload");
InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File(root, fileFileName)); System.out.println("fileFileName:" + fileFileName);
// 因为file是存放在临时文件夹的文件,我们可以将其文件名和文件路径打印出来,看和之前的fileFileName是否相同
System.out.println("file:" + file.getName());
System.out.println("file:" + file.getPath()); 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 SUCCESS;
}
}
2.多文件上传
jsp页面: <script type="text/javascript">
$(function() {
$("#button").click(function (){
var html = $("<input type='file' name='file'/><br/>");
var button = $("<input type='button' name='button' value='删除'/><br/>");
$("#body div").append(html).append(button);
button.click(function (){
html.remove();
button.remove();
});
});
});
</script>
<body id="body">
<!-- 多文件上传 -->
<form action="manyFileupload.action" method="post"
enctype="multipart/form-data">
username:
<input type="text" name="username" />
<br />
file:
<input type="file" name="file" />
<br />
<input type="button" value="添加" id="button" />
<br />
<div></div>
<input type="submit" value="提交" />
</form>
</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;
} @Override
public 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.文件下载
jsp页面 <!-- 下载 -->
<form action="fileDownload.action">
<input type="submit" value="下载" />
</form>
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中选择一张图片进行下载
} @Override
public String execute() throws Exception {
return super.execute();
}
}
4.struts.xml
<!-- 定义全局结果:必须写在action上面 -->
<global-results>
<result name="success">/success.jsp</result>
</global-results> <!-- 单文件上传 -->
<action name="Fileupload" class="com.Struts2.action.Fileupload"></action>
<!-- 多文件上传-->
<action name="manyFileupload" class="com.Struts2.action.ManyFileupload"></action>
<!-- 文件下载 -->
<action name="fileDownload" class="com.Struts2.action.FileDownload">
<result name="success" type="stream">
<param name="contentDisposition">attachment;filename="psb.jpg"</param><!-- attachment表示弹出下载提示框 -->
<param name="inputName">downloadFile</param><!-- downloadFile对应action中的getDownloadFile方法 -->
</result>
</action>
本博客源码出自:http://www.cnblogs.com/xiaoluo501395377/archive/2012/10/26/2740882.html。谢谢他的分享!
Struts2中文件上传下载实例的更多相关文章
- jsp\struts1.2\struts2 中文件上传(转)
jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...
- Struts2实现文件上传下载功能(批量上传)
今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...
- linux中文件上传下载
windows篇 linux文件下载到windows sz命令 登录到linux服务器使用 sz log.log 命令,弹出对话框选择下载文件的目录,点击确定即可. windows文件上传到linux ...
- JAVA中使用FTPClient实现文件上传下载实例代码
一.上传文件 原理就不介绍了,大家直接看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...
- Struts2之文件上传下载
本篇文章主要介绍如何利用struts2进行文件的上传及下载,同时给出我在编写同时所遇到的一些问题的解决方案. 文件上传 前端页面 <!-- 引入struts标签 --> <%@tag ...
- Struts2 文件上传,下载,删除
本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...
- Struts2 控制文件上传下载
之前介绍servlet3.0新特性的时候有提到过servlet API提供了一个part类来实现对文件的上传和保存,Struts其实是在其基础上做了进一步的封装,更加简单易用.至于文件下载,Strut ...
- struts2中文件上传
注意点 private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的 private String imageFileName;// ...
- plupload+struts2实现文件上传下载
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" ...
随机推荐
- 【python练习题】程序13
#题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153= ...
- CUDA开发
CUB库 https://nvlabs.github.io/cub/index.html
- ACM之路——上车了
校赛坚持到底,拿到了银牌:第一批进入ACM队集训,期末考试之前仍然代码不断,甚至感觉对不起大学第一次的期末考试,五天复习高数,两天复习英语,看到英语成绩是胸口突然好痛,好难受……就为了成为ACM正式队 ...
- 遍历map中的内容
Map<String, CartItem> cartItems = cart.getCartItems();for(Map.Entry<String, CartItem> en ...
- 使用Promise解决多层异步调用的简单学习【转】
前言 本文章转载文章: https://www.jianshu.com/p/29da9aef4c1c 第一次接触到Promise这个东西,是2012年微软发布Windows8操作系统后抱着作死好奇的心 ...
- ElasticHD Windows环境下安装
ElasticHD Linux环境下安装教程 ElasticHD windows环境下安装教程 习惯了T-SQL 查询,Elasticsearch的DSL查询语法简直就是反人类呀,一 ...
- Matplotlib学习---用matplotlib画直方图/密度图(histogram, density plot)
直方图用于展示数据的分布情况,x轴是一个连续变量,y轴是该变量的频次. 下面利用Nathan Yau所著的<鲜活的数据:数据可视化指南>一书中的数据,学习画图. 数据地址:http://d ...
- Android工程图片资源命名禁忌
Android工程中,res\drawable\ 文件夹下所有的图片资源文件命名,不允许: 1. 大写字母 从Eclipse的这个报错可以知道资源文件的命名规则. Invalid file name: ...
- a span做成按钮样式不选中文字
a,span做成按钮样式时,文字会被选中.加以下CSS可以让其不选中.测试三大浏览器都可以 .button { display: inline-block; -moz-user-select: non ...
- 【原创】线段树query模板对比! 新手线段树的一个容易出错的问题!!因为我就糊涂了一整天.......
我们解决问题的最好方法就是拿实例来举例子 我们来看tyvj1038或计蒜客 “管家的忠诚” 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记k次账,由于管 ...