一 文件上传

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在上面。

最新文章

  1. python之 sqlalchemy
  2. FPGA 相同模块 VIVADO synthesis综合后
  3. longjmp setjmp and volatile
  4. 获取当前请求的URL的地址、参数、参数值、各种属性
  5. SQL笔记 - CTE递归实例:显示部门全称
  6. JQuery实战图片特效-遁地龙卷风
  7. ubuntu中jdk已经安装,但是eclipse启动报错
  8. Balsamiq Mockups 注册码
  9. GCD的基本知识
  10. MySql获取表的字段名称、字段注解、字段类型、字段长度
  11. 6.python模块(导入,内置,自定义,开源)
  12. 从网页上抓取Windows补丁信息然后整型输出(PowerShell)
  13. Linux 打包和压缩 方法详解
  14. WPF 画刷应用
  15. [转] Linux中gcc,g++常用编译选项
  16. libthrift0.9.0解析(二)之TSimpleServer
  17. smarty模板基础3 *缓存数据*
  18. 控制结构(2) 卫语句(guard clause)
  19. Assignments
  20. Android中JNI编程详解

热门文章

  1. batch、随机、Mini-batch梯度下降
  2. php pdo 获取数据转换为json
  3. VSCode 常用setiings.json设置
  4. Codeforces Round #567 (Div. 2)自闭记
  5. rem换算公式
  6. css---5 only-child or nth-of-type
  7. CF930E Coins Exhibition
  8. Spring知识点整理
  9. ASCII, Unicode 与 UTF-8
  10. ECMAScript 6中的Set和Map数据结构