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中文件上传下载实例的更多相关文章

  1. jsp\struts1.2\struts2 中文件上传(转)

    jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...

  2. Struts2实现文件上传下载功能(批量上传)

    今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...

  3. linux中文件上传下载

    windows篇 linux文件下载到windows sz命令 登录到linux服务器使用 sz log.log 命令,弹出对话框选择下载文件的目录,点击确定即可. windows文件上传到linux ...

  4. 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 ...

  5. Struts2之文件上传下载

    本篇文章主要介绍如何利用struts2进行文件的上传及下载,同时给出我在编写同时所遇到的一些问题的解决方案. 文件上传 前端页面 <!-- 引入struts标签 --> <%@tag ...

  6. Struts2 文件上传,下载,删除

    本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...

  7. Struts2 控制文件上传下载

    之前介绍servlet3.0新特性的时候有提到过servlet API提供了一个part类来实现对文件的上传和保存,Struts其实是在其基础上做了进一步的封装,更加简单易用.至于文件下载,Strut ...

  8. struts2中文件上传

    注意点 private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的 private String imageFileName;// ...

  9. plupload+struts2实现文件上传下载

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8" ...

随机推荐

  1. Centos使用虚拟环境创建python django工程

    本地环境 通常我们登录就是后就是本地环境 本地环境下查看pip安装了那些包 pip3 list 可以看到本地环境下我们安装的是django1.11.16版本,现在我有个项目要使用django 2.0以 ...

  2. PHP——敏感词过滤

    前言 如果可以用第三方的话,那么你是幸运的,因为现在这种敏感词过滤,敏感图片,敏感语音过滤的第三方服务还是挺多的 敏感词过滤 核心代码 利用PHP内置的三个函数 array_combine() | a ...

  3. BZOJ 2049 洞穴勘测

    LCT判断联通性 没什么特别的..还是一个普通的板子题,把LCT当并查集用了,只不过LCT灵活一些,还可以断边 话说自从昨天被维修数列那题榨干之后我现在写splay都不用动脑子了,,机械式的码spla ...

  4. bzoj 2957 楼房重建 (线段树+思路)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2957 思路: 用分块可以很简单的过掉,但是这道题也可以用线段树写. 分类讨论左区间最大值对 ...

  5. MT【289】含参绝对值的最大值之三

    已知$a>0$,函数$f(x)=e^x+3ax^2-2e x-a+1$,(1)若$f(x)$在$[0,1]$上单调递减,求$a$的取值范围.(2)$|f(x)|\le1$对任意$x\in[0,1 ...

  6. ATR102E Stop. Otherwise... [容斥]

    第一道容斥 \(ans[i] = \sum_{j = 0}^{min(cnt, n / 2)} (-1)^j \tbinom{cnt}{j} \tbinom{n - 2*j + k - 1}{k - ...

  7. Codeforces Round #543 Div1题解(并不全)

    Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...

  8. PHP-FPM安装报错解决

    PHP源码安装 setenforce 0--------------------------------------------------------------------安装php时的报错che ...

  9. python中,print函数的sep和end参数

    print函数是我们经常使用的,但是它的sep和end参数或许对很多python使用者相对陌生,他们可以让我们的打印更具有个性化. 先来看下官方解释, sep:分割值与值,默认是一个空格 end:附件 ...

  10. LVS搭建负载均衡(二)DR模型

    应用场景:LVS配置负载均衡方式之一:dr 测试环境: 配置步骤: 1. 在主机lvs上安装ipvsadm ~]# yum install ipvsadm -y ~]# ipvsadm //启动:该命 ...