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" ...
随机推荐
- Opencv画图操作
1. 画矩形 MyRect rect;rect.left = 5;rect.top = 5;rect.right = 100;rect.bottom = 100;IplImage * pColorIm ...
- Build 2017 Revisited: .NET, XAML, Visual Studio
For the next couple months we're going to revisit Build 2017, each post focusing on different aspect ...
- PlaNet,使用图像输入来学习世界模型
Google AI团队与DeepMind合作,上周宣布了一个名为PlaNet的新的开源“Deep Planning”网络. PlaNet是一个人工智能代理,它只使用图像输入来学习世界模型,并使用这些模 ...
- 前端部分-CSS基础介绍
CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素.也就是定义相应的标签语言来定制显示样式达到一定的显示效果. 每个CSS样式由两个组成部分:选择器和 ...
- ie11的版本判断
我的电脑昨天更新的时候把ie11给更新出来了,然后发现我的skylineweb项目提示我的浏览器不是ie,这样显然是浏览器检测出现了问题.查找后找到了下面的解决方法.大家的电脑如果也更新成了ie11的 ...
- Spring 使用介绍(十二)—— Spring Task
一.概述 1.jdk的线程池和任务调用器分别由ExecutorService.ScheduledExecutorService定义,继承关系如下: ThreadPoolExecutor:Executo ...
- #186 path(容斥原理+状压dp+NTT)
首先只有一份图时显然可以状压dp,即f[S][i]表示S子集的哈密顿路以i为终点的方案数,枚举下个点转移. 考虑容斥,我们枚举至少有多少条原图中存在的边(即不合法边)被选进了哈密顿路,统计出这个情况下 ...
- 第三十八天 GIL 进程池与线程池
今日内容: 1.GIL 全局解释器锁 2.Cpython解释器并发效率验证 3.线程互斥锁和GIL对比 4.进程池与线程池 一.全局解释器锁 1.GIL:全局解释器锁 GIL本质就是一把互斥锁,是夹在 ...
- MT【246】方程根$\backsim$图像交点
已知函数$f(x)=x^2+x-2$,若$g(x)=|f(x)|-f(x)-2mx-2m^2$ 有三个不同的零点,则$m$的取值范围_____ 分析:等价于$h(x)=|f(x)|-f(x),t(x) ...
- 【题解】 bzoj1135: [POI2009]Lyz (线段树+霍尔定理)
题面戳我 Solution 二分图是显然的,用二分图匹配显然在这个范围会炸的很惨,我们考虑用霍尔定理. 我们任意选取穿\(l,r\)的号码鞋子的人,那么这些人可以穿的鞋子的范围是\(l,r+d\),这 ...