四、多个文件上传:

五、struts2文件下载:

多个文件上传action

com.cy.action.FilesUploadAction.java:

package com.cy.action;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FilesUploadAction extends ActionSupport{
private static final long serialVersionUID = 1L; private File[] struts; //文件,对应fileupload.jsp中input type=file的name
private String[] strutsFileName; //文件名
private String[] strutsContentType; //文件类型 public File[] getStruts() {
return struts;
} public void setStruts(File[] struts) {
this.struts = struts;
} public String[] getStrutsFileName() {
return strutsFileName;
} public void setStrutsFileName(String[] strutsFileName) {
this.strutsFileName = strutsFileName;
} public String[] getStrutsContentType() {
return strutsContentType;
} public void setStrutsContentType(String[] strutsContentType) {
this.strutsContentType = strutsContentType;
} @Override
public String execute() throws Exception {
for(int i=0; i<struts.length; i++){
System.out.println("文件名:" + strutsFileName[i]);
System.out.println("文件类型:" + strutsContentType[i]);
String filePath = "E:/" + strutsFileName[i];
File savefile = new File(filePath);
FileUtils.copyFile(struts[i], savefile);
}
return SUCCESS;
} }

文件下载action

FileDownloadAction.java:

package com.cy.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; import com.opensymphony.xwork2.ActionSupport; public class FileDownloadAction extends ActionSupport{ private static final long serialVersionUID = 1L;
private String fileName; //下载的文件名 public String getFileName() throws Exception {
fileName=new String(fileName.getBytes(),"ISO8859-1"); //对文件中文名进行重编码
return fileName;
}
public void setFileName(String fileName){
this.fileName = fileName;
} /**
* 返回的是文件流
* @return
* @throws Exception
*/
public InputStream getInputStream() throws Exception{
File file = new File("E:/1.jpg"); //将E盘下的1.png下载下来
this.fileName = "美女哇哇.png"; //重新定义文件名
return new FileInputStream(file); //返回inputStream
}
}

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 配置允许上传文件最大为20000000Byte,约20Mb -->
<constant name="struts.multipart.maxSize" value="20000000"></constant> <package name="manage" extends="struts-default">
<action name="upload" class="com.cy.action.FileUploadAction">
<result name="input">fileupload.jsp</result>
<result name="success">success.jsp</result> <!-- 配置允许上传的文件类型
配置允许上传的文件大小,最大81101byte,= 79.2KB
-->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param>
<param name="maximumSize">81101</param>
</interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref>
</action> <action name="uploads" class="com.cy.action.FilesUploadAction">
<result name="input">filesupload.jsp</result>
<result name="success">success.jsp</result>
</action> <action name="download" class="com.cy.action.FileDownloadAction">
<result type="stream">
<param name="contentDisposition">attachment;filename=${fileName}</param>
</result>
</action>
</package> </struts>

多个文件上传filesupload.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="uploads" method="post" enctype="multipart/form-data">
选择文件1:<input type="file" name="struts" /><br/>
选择文件2:<input type="file" name="struts" /><br/>
选择文件3:<input type="file" name="struts" /><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>

文件上传成功success.jsp:

<body>
文件上传成功!
</body>

文件下载filedownload.jsp:

<body>
<a href="download">文件下载</a>
</body>

测试结果:

1.多个文件上传:

选择文件上传,成功后console打印:

2.文件下载,将E盘下的1.jpg下载下来:

最新文章

  1. 执行 $Gulp 时发生了什么 —— 基于 Gulp 的前端集成解决方案(二)
  2. ASP.NET MVC下的四种验证编程方式[续篇]
  3. poj 1141
  4. foreach的用法
  5. ExtJS笔记 Store
  6. WinCE开机Logo的实现(USB下载图片到nandflash)
  7. 转】用Maven构建Mahout项目
  8. JAVA IO详解
  9. 01_docker学习总结
  10. 为Chrome添加https搜索 自定义地址栏搜索引擎
  11. PHP开发之路之一--WAMP的安装和配置
  12. Apache Bench安装与使用
  13. 对Spring事务的一些误解
  14. prime distance on a tree(点分治+fft)
  15. SSR搭建服务器
  16. js设计模式(六)---命令模式
  17. CUDA 编程的基本模式
  18. [C#]“正在终止线程”的问题
  19. 关于java中的使用通配符错误,错误信息Diamond types are not supported at language level &#39;5‘
  20. Easyui入门视频教程 第10集---Messager的使用

热门文章

  1. 关于TCP/IP协议
  2. New Concept English there (5)
  3. New Concept English there (3)
  4. Sqlserver 存储过程 返回-6
  5. C语言 运算符详细介绍及示例代码
  6. 用JQuery怎么去一次性获取 aspTextBox 文本框的值
  7. Visual studio 生成后事件说明
  8. Kotlin Reference (七) Returns and Jumps
  9. Swift 保存pdf并在webview显示
  10. Android常见问题——Genymotion无法启动问题