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" ...
随机推荐
- Centos安装python3
安装环境 系统:阿里云服务器centos7.5系统 看见好多博客对centos安装python3的方式各不相同且都不完整,今天我来完整的演示安装python3 1.下载python3源码包 命令 wg ...
- x = x &(x-1)
判断一个数(x)的二进制含有几个1 int func(x) { ; while(x) { countx ++; x = x&(x-); } return countx; } 假定x = 999 ...
- BZOJ3502PA2012Tanie linie&BZOJ2288[POJ Challenge]生日礼物——模拟费用流+链表+堆
题目描述 n个数字,求不相交的总和最大的最多k个连续子序列. 1<= k<= N<= 1000000. 输入 输出 样例输入 5 2 7 -3 4 -9 5 样例输出 13 根据 ...
- POJ 1017 最少包裹
参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6414760.html Packets Time Limit: 1000MS Memory Li ...
- Python代码编写规范
Python代码编写规范 编码: a) 如无特殊情况,文件一律使用UTF-8编码 b) 如无需特殊情况,文件头部必须加入#-*-coding:utf-8-*- 缩进 a) 统一 ...
- TP5调用微信JSSDK 教程 —— 之异步使用
细节请参考前一篇文章:JSSDK.PHP 修改下: <?php namespace jssdk; class Jssdk { private $appId; private $appSecret ...
- 求集合中选一个数与当前值进行位运算的max
求集合中选一个数与当前值进行位运算的max 这是一个听来的神仙东西. 先确定一下值域把,大概\(2^{16}\),再大点也可以,但是这里就只是写写,所以无所谓啦. 我们先看看如果暴力求怎么做,位运算需 ...
- loj6157 A ^ BProblem (并查集)
设s[x][i]表示从根到x的异或和在第i位上的值(0/1),(a,b,i)表示a到b的异或和在第i位上的值那么就有(a,b,i)=(s[a][i]^s[b][i]^s[lca][i]^s[lca][ ...
- Baker Vai LightOJ - 1071 (MCMF)
在个给出的矩阵从,从左上角走到右下角,然后再从右下角走到左上角,两次不能经过想同的点,每个点都有一个价值,问最大的价值是多少. 可以把原来的问题化简成从左上角走两条路到右下角,然后把价值加起来,然是这 ...
- IO创建Socket通信中慎用BufferReader中的readLine()
在编写Socket的Demo的时候,在Server中使用BufferReader获取从客服端发送过来的内容 package cn.lonecloud.socket; import cn.loneclo ...