一 文件上传

1.环境要求

commons-fileupload-xxx.jar

commons-io-xxx.jar

2.准备jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>
<s:form action="Fileupload" method="" enctype="multipart/form-data">
<s:textfield name="title" label="用户名"/><br>
<s:file name="upload" label="上传文件"/><br>
<s:submit name="submit" value="上传" />
</s:form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>
<s:form action="FileuploadMore" method="post" enctype="multipart/form-data">
<s:textfield name="title" label="用户名"/><br>
<s:file name="upload" label="上传文件"/><br>
<s:file name="upload" label="上传文件"/><br>
<s:file name="upload" label="上传文件"/><br>
<s:file name="upload" label="上传文件"/><br>
<s:submit name="submit" value="上传" />
</s:form>
</body>
</html>

3.Action页面

package com.icss.action.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /**
***************************
*@类名 UploadAction.java public class UploadAction extends ActionSupport{ //1.普通字段name属性名
private String title;
private File upload;//file 控件名 它要和表单name属性和action属性名要一致
//2.控件名+ContentType
private String uploadContentType;//上传文件的类型
//3.控件名+FileName
private String uploadFileName;//上传文件的名称
//4.上传文件的路径 upload
private String savePath; //获取完整的路径 //上传
public String execute() throws Exception {
byte[] buffer = new byte[2048];//缓冲区
//输入流
FileInputStream fis = new FileInputStream(upload);
//输出流 \ \
FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+uploadFileName);
int len=fis.read(buffer);
while(len>0){
fos.write(buffer, 0, len);
len=fis.read(buffer);
}
fos.flush();
fos.close();
fis.close();
return "success"; } //生成get和set方法
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
} }
public class UploadMoreAction extends ActionSupport{
//file 控件 name 属性和action属性名要一致
//1.普通字段name属性名
private String title;
private File[] upload;
//2.控件名+ContentType
private String[] uploadContentType;//上传文件的类型
//3.控件名+FileName
private String[] uploadFileName;//上传文件的名称
//4.上传文件的路径 upload
private String savePath; //获取完整的路径 //上传
public String execute() throws Exception {
byte[] buffer = new byte[2048];//缓冲区
for(int i=0;i<upload.length;i++){
//输入流
FileInputStream fis = new FileInputStream(upload[i]);
//输出流 \ \
FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+uploadFileName[i]);
int len=fis.read(buffer);
while(len>0){
fos.write(buffer, 0, len);
len=fis.read(buffer);
}
fos.flush();
fos.close();
fis.close(); }
return "success";
} public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
} public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
} public File[] getUpload() {
return upload;
} public void setUpload(File[] upload) {
this.upload = upload;
} public String[] getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
} public String[] getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}

4.配置struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="false"/>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.configuration.xml.reload" value="true"/>
<package name="upload" namespace="/" extends="struts-default">
<!-- 单文件上传 -->
<action name="Fileupload" class="com.icss.action.upload.UploadAction">
<param name="savePath">/upload</param>
<result name="success">/upload_success.jsp</result>
</action>
<!-- 多文件上传配置 -->
<action name="FileuploadMore" class="com.icss.action.upload.UploadMoreAction">
<param name="savePath">/upload</param>
<result name="success">/upload_success.jsp</result>
</action>
<!-- 下载文件 -->
<action name="download" class="com.icss.action.upload.downloadAction">
<param name="inputPath">/upload</param>
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputname">inputStream</param>
<param name="contentDisposition">
attachment;filename="${fileName}"
</param>
<param name="bufferSize">4096</param>
</result>
</action> </package>
</struts>

File类型的xxx属性,与表单中的File控件的name属性一致,用于封装File控件对应的文件内容

String类型的xxxFileName属性,该属性名称就是前面的File类型属性+FileName组合而成,是固定的语法,作用就是封装File控件对应文件的文件名

String类型的xxxContentType属性,同样也是xxx属性+ContentType组合而成,固定语法,作用就是封装File控件对应文件的文件类型

在Action中设置了这三个属性,在执行上传时就可以直接通过getXxx()方法来获取到上传文件的文件名、类型及文件内容。

而实现文件上传的过程就是使用流实现文件读取的过程,

二 文件下载

jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<a href="download?fileName=head.jpg">下载此图片</a>
</body>
</html>

2.Action

public class downloadAction extends ActionSupport {
//下载路劲
private String inputPath;
//下载文件名
private String fileName;
//读取下载文件的流
private InputStream inputname;
//获取输入流
public InputStream getInputName() throws FileNotFoundException {
String path = ServletActionContext.getServletContext().getRealPath(inputPath);
return new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
} public String getInputPath() {
return inputPath;
} public void setInputPath(String inputPath) {
this.inputPath = inputPath;
} public String getFileName() {
return fileName;
} public void setFilename(String fileName) {
this.fileName = fileName;
} public void setInputName(InputStream inputName) {
this.inputname = inputName;
} public String execute() throws Exception { return SUCCESS;
}
}

struts.xml在上面。

struts2文件上传(多文件)文件下载的更多相关文章

  1. struts2文件上传,文件类型 allowedTypes

    struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...

  2. SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...

  3. SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库

    SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库  /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...

  4. webAPI文件上传时文件过大404错误的问题

    背景:最近公司有个需求,外网希望自动保存数据到内网,内网有2台服务器可以相互访问,其中一台服务器外网可以访问,于是想在 这台服务器上放个中转的接口.后来做出来以后测试发现没有问题就放线上去了,不顾发现 ...

  5. struts2实现文件上传(多文件上传)及下载

    一.要实现文件上传,需在项目中添加两个jar文件 二.上传准备的页面 注:必须植入enctype="multipart/form-data"属性,以及提交方式要设置成post &l ...

  6. Struts---多文件上传、单文件下载

    struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUB ...

  7. Struts2 单个文件上传/多文件上传

    1导入struts2-blank.war所有jar包:\struts-2.3.4\apps\struts2-blank.war 单个文件上传 upload.jsp <s:form action= ...

  8. Struts2文件上传--多文件上传(插件uploadify)

    公司需要把以前的Struts2自带的图片上传替换掉,因为不能一个file选择多个文件,本人直接百度搜索图片插件,  貌似就它(uploadify3.2.1)在最前面,也找过很多案例, 其中有不少问题, ...

  9. [转]SpringMVC单文件上传、多文件上传、文件列表显示、文件下载

    一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...

  10. Struts2之文件上传(单文件/多文件)

    <一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...

随机推荐

  1. 十个非常实用的MySQL命令

      建赟  版主 楼主   前言 今天介绍一些MySQL常用的实用命令,都是一些比较简单的命令.已经知道的朋友,就当是巩固吧,不知道的童鞋,可以好好在自己的机器上,练习下. 0. 显示数据库 命令:s ...

  2. 既然 start() 方法会调用 run() 方法,为什么我们调用 start() 方法,而不直接调用 run() 方法?

    当你调用 start() 方法时,它会新建一个线程然后执行 run() 方法中的代码.如果直接调用 run() 方法,并不会创建新线程,方法中的代码会在当前调用者的线程中执行

  3. Laravel 迁移检查表是否存在

    Schema::hasTable('TableName'); //检查表释放存在 Schema::hasColumn('tableName', 'columeName'); //检查表是否存在某个字段 ...

  4. python3文件方法

    Python3 File(文件) 方法 需要注意:打开文件的 mode 参数注意区别文件指针位置 rw/rw+ 文件指针是在文件的开头,w任何模式都是会删除之前原有的内容的, a是追加且,打开文件指针 ...

  5. thinkphp url生成

    为了配合所使用的URL模式,我们需要能够动态的根据当前的URL设置生成对应的URL地址,为此,ThinkPHP内置提供了U方法,用于URL的动态生成,可以确保项目在移植过程中不受环境的影响. 定义规则 ...

  6. C++——多态

    1. 多态定义的构成条件 多态是在不同继承关系的类对象,去调同一函数,产生了不同的行为. 就是说,有一对继承关系的两个类,这两个类里面都有一个函数且名字.参数.返回值均相同,然后我们通过调用函数来实现 ...

  7. 怎样配置duilib

    duilib是一个免费的界面库,它可利用xml文件自定义界面元素,并且可以在商业项目中无偿使用.怎样在VS中配置duilib界面库呢?请看下面的介绍. 工具/原料 duilib 下载和编译duilib ...

  8. (转)H264--1--编码原理以及I帧B帧P帧 .

    转:http://blog.csdn.net/yangzhongxuan/article/details/8003504 ---------------------- 前言 ------------- ...

  9. 机器学习 101 Mahout 简介 建立一个推荐引擎 使用 Mahout 实现集群 使用 Mahout 实现内容分类 结束语 下载资源

      机器学习 101 Mahout 简介 建立一个推荐引擎 使用 Mahout 实现集群 使用 Mahout 实现内容分类 结束语 下载资源 相关主题   在信息时代,公司和个人的成功越来越依赖于迅速 ...

  10. JS之缓冲动画

    原素材 main.html <!DOCTYPE html> <html lang="en"> <head> <link href=&quo ...