(一)单个文件的上传步骤:

1.拷贝jar包:commons-fileupload.jar,  commons-io.jar

下载链接(文件上传.rar):http://www.cnblogs.com/withyou/admin/Files.aspx

2.JSP页面中增加form:(form属性是固定的)

<form action= "uploadAction"    enctype="multipart/form-data" method="post"  >
姓名:<input type="text" name="uname" /><br/>
附件:<input type="file" name="fileUpload" /><br/>
<input type="submit" value="提交" />
</form>

3.action层代码:

 public class UploadAction {
private String uname;
private File fileUpload;
private String fileUploadFileName;//名字规范 File属性的 名字+FileName,(该属性为上传过来的文件名)
public String execute() throws IOException{
String path = ServletActionContext.getServletContext().getRealPath("/fileUpload/");//该path为tomcat下的webapp/工程/下
for(int i = 0;i<fileUpload.length;i++){
String[] fielNameArray = fileUploadFileName[i].split("\\.");
//将上传的文件存到指定路径下,UUID是为了避免文件名重复
FileUtils.copyFile(fileUpload[i],new File(path+"\\"+UUID.randomUUID()+"."+fielNameArray[fielNameArray.length-1]));
}
return null;
}
public String getUname() {
return uname;
} public void setUname(String uname) {
this.uname = uname;
} public File[] getFileUpload() {
return fileUpload;
} public void setFileUpload(File[] fileUpload) {
this.fileUpload = fileUpload;
} public String[] getFileUploadFileName() {
return fileUploadFileName;
} public void setFileUploadFileName(String[] fileUploadFileName) {
this.fileUploadFileName = fileUploadFileName;
}
}

4.补充几个java获取路径的信息
  a.获取web服务器下的文件路径 (tomcat等服务器安装目录/webapp/工程/)
    request.getRealPath("/") ;application.getRealPath("")【jsp中 】 ;ServletContext().getRealPath("")

    如:request.getRealPath("web.xml") 的结果为:
    C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\strutsTest\web.xml

  b.获取本地路径

    this.getClass().getClassLoader().getResource("").getPath(); ==/D:/workspace/strutsTest/WebRoot/WEB-INF/classes/ 
    this.getClass().getResource("").getPath().toString(); ==/D:/workspace/strutsTest/WebRoot/WEB-INF/classes/bl/

  c.获取相对路径 :

    request.getContextPath();

(二)多个文件上传步骤:

 1.拷贝jar包;

 2.jsp代码如下:

  

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'upload.jsp' starting page</title>
<script src="js/jquery-1.4.4.js"></script>
<script type="text/javascript">
/*添加附件*/
function addFileButton(){
var fileStr = "<div>附件:<input type='file' name='fileUpload' /> <input type='button' value='删除' onclick='delFileButton(this);' /><br/></div>";
$("#div1").append(fileStr);
}
/*删除附件*/
function delFileButton(obj){
obj.parentNode.parentNode.removeChild(obj.parentNode);
}
</script>
</head>
<body>
<form action= "uploadAction" enctype="multipart/form-data" method="post" >
姓名:<input type="text" name="uname" /><br/>
<fieldset>
<legend><input type="button" value="添加附件" onclick="addFileButton();" /></legend>
<div id="div1">
<div>附件:<input type="file" name="fileUpload" /><input type="button" value="删除" onclick="delFileButton(this);" /><br/></div>
</div>
</fieldset>
<input type="submit" value="提交" />
</form>
</body>
</html>

 3.action层代码:

 package com.bjsxt.struts2.action;

 import java.io.File;
import java.io.IOException;
import java.util.UUID; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; public class UploadAction {
private String uname;
private File[] fileUpload;
private String[] fileUploadFileName;//名字规范 File属性的 名字+FileName;
public String execute() throws IOException{
System.out.println("--------uname-------"+uname);
String path = ServletActionContext.getServletContext().getRealPath("/fileUpload/");
System.out.println(path);
for(int i = 0;i<fileUpload.length;i++){
String[] fielNameArray = fileUploadFileName[i].split("\\.");
FileUtils.copyFile(fileUpload[i],new File(path+"\\"+UUID.randomUUID()+"."+fielNameArray[fielNameArray.length-1]));
}
return null;
}
public String getUname() {
return uname;
} public void setUname(String uname) {
this.uname = uname;
} public File[] getFileUpload() {
return fileUpload;
} public void setFileUpload(File[] fileUpload) {
this.fileUpload = fileUpload;
} public String[] getFileUploadFileName() {
return fileUploadFileName;
} public void setFileUploadFileName(String[] fileUploadFileName) {
this.fileUploadFileName = fileUploadFileName;
}
}

(三)文件下载:

1.action层代码:

 package com.bjsxt.hib.action;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException; public class DownloadAction {
private InputStream inputStream;
private String fileName; public String execute(){
try {
this.inputStream = new FileInputStream("D:\\123.txt");//这里有很多处理手段满足不同需求(从数据库读取等)
this.fileName = "哈喽.txt";
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return "success";
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
/*乱码处理*/
public String getFileName() {
String temp=null;
try {
temp = new String(this.fileName.getBytes(),"ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return temp;
}
public void setFileName(String fileName) {
this.fileName = fileName;
} }

2.struts.xml配置:

  <action name="downloadAction" class="com.bjsxt.hib.action.DownloadAction">
<result name="success" type="stream">
<!--前两行是不可变的 -->
<param name="contentType">application/x-download</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">4096</param>
</result>
</action>

3.浏览器直接输入:http://localhost:8080/downloadAction即可访问,下载.

最新文章

  1. PAT 1009. 说反话 (20) JAVA
  2. RESTful简单介绍(入门)
  3. C++获取鼠标位置及全局检测鼠标行为
  4. SNMP远程监控进程信息的OID
  5. Swift中的数组
  6. python数字图像处理(14):高级滤波
  7. MySQL存储过程循环添加数据
  8. hdu 2853 Assignment KM算法
  9. 大数据时代下的用户洞察:用户画像建立(ppt版)
  10. Mac配置JAVA_HOME
  11. Gridview全选
  12. 关于Apache Struts 2 S2-032高危漏洞的一些确认
  13. 进程关系之tcgetpgrp、tcsetpgrp和tcgetsid函数
  14. oracle_执行计划_谓词信息和数据获取(access and filter区别) (转)
  15. iOS UITabBar
  16. zTree实现地市县三级级联Service接口测试
  17. That girl
  18. visual studio 2017 installer 安装包制作过程出现的问题---无法注册模块 HRESULT -2147024769 请与您的技术支持人员联系
  19. [Java] HashMap 源码简要分析
  20. w命令 查看系统负载

热门文章

  1. Cassandra C++/NodeJs开发环境
  2. CSS下背景属性background的使用方法
  3. Windows WDDM显卡驱动框架及GPUView工具的使用(1)
  4. 一些javascript常用方法
  5. JavaScript继承基础讲解,原型链、借用构造函数、混合模式、原型式继承、寄生式继承、寄生组合式继承
  6. Java 多线程之内存一致性错误
  7. C# 解析bt种子
  8. solr与.net主从复制
  9. 让你的ubuntu串口程序可以直接读写串口
  10. javascript脚本化文档