(一)单个文件的上传步骤:
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层代码:
1 public class UploadAction { 2 private String uname; 3 private File fileUpload; 4 private String fileUploadFileName;//名字规范 File属性的 名字+FileName,(该属性为上传过来的文件名) 5 public String execute() throws IOException{ 6 String path = ServletActionContext.getServletContext().getRealPath("/fileUpload/");//该path为tomcat下的webapp/工程/下 7 for(int i = 0;i<fileUpload.length;i++){ 8 String[] fielNameArray = fileUploadFileName[i].split("\."); 9 //将上传的文件存到指定路径下,UUID是为了避免文件名重复 10 FileUtils.copyFile(fileUpload[i],new File(path+"\"+UUID.randomUUID()+"."+fielNameArray[fielNameArray.length-1])); 11 } 12 return null; 13 } 14 public String getUname() { 15 return uname; 16 } 17 18 public void setUname(String uname) { 19 this.uname = uname; 20 } 21 22 public File[] getFileUpload() { 23 return fileUpload; 24 } 25 26 public void setFileUpload(File[] fileUpload) { 27 this.fileUpload = fileUpload; 28 } 29 30 public String[] getFileUploadFileName() { 31 return fileUploadFileName; 32 } 33 34 public void setFileUploadFileName(String[] fileUploadFileName) { 35 this.fileUploadFileName = fileUploadFileName; 36 } 37 }
4.补充几个java获取路径的信息
a.获取web服务器下的文件路径 (tomcat等服务器安装目录/webapp/工程/)
request.getRealPath("/") ;application.getRealPath("")【jsp中 】 ;ServletContext().getRealPath("")
如:request.getRealPath("web.xml") 的结果为:
C:Program FilesApache Software FoundationTomcat 5.5webappsstrutsTestweb.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代码如下:
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 <title>My JSP 'upload.jsp' starting page</title> 12 <script src="js/jquery-1.4.4.js"></script> 13 <script type="text/javascript"> 14 /*添加附件*/ 15 function addFileButton(){ 16 var fileStr = "<div>附件:<input type='file' name='fileUpload' /> <input type='button' value='删除' onclick='delFileButton(this);' /><br/></div>"; 17 $("#div1").append(fileStr); 18 } 19 /*删除附件*/ 20 function delFileButton(obj){ 21 obj.parentNode.parentNode.removeChild(obj.parentNode); 22 } 23 </script> 24 </head> 25 <body> 26 <form action= "uploadAction" enctype="multipart/form-data" method="post" > 27 姓名:<input type="text" name="uname" /><br/> 28 <fieldset> 29 <legend><input type="button" value="添加附件" onclick="addFileButton();" /></legend> 30 <div id="div1"> 31 <div>附件:<input type="file" name="fileUpload" /><input type="button" value="删除" onclick="delFileButton(this);" /><br/></div> 32 </div> 33 </fieldset> 34 <input type="submit" value="提交" /> 35 </form> 36 </body> 37 </html>
3.action层代码:
1 package com.bjsxt.struts2.action; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.UUID; 6 7 import org.apache.commons.io.FileUtils; 8 import org.apache.struts2.ServletActionContext; 9 10 public class UploadAction { 11 private String uname; 12 private File[] fileUpload; 13 private String[] fileUploadFileName;//名字规范 File属性的 名字+FileName; 14 public String execute() throws IOException{ 15 System.out.println("--------uname-------"+uname); 16 String path = ServletActionContext.getServletContext().getRealPath("/fileUpload/"); 17 System.out.println(path); 18 for(int i = 0;i<fileUpload.length;i++){ 19 String[] fielNameArray = fileUploadFileName[i].split("\."); 20 FileUtils.copyFile(fileUpload[i],new File(path+"\"+UUID.randomUUID()+"."+fielNameArray[fielNameArray.length-1])); 21 } 22 return null; 23 } 24 public String getUname() { 25 return uname; 26 } 27 28 public void setUname(String uname) { 29 this.uname = uname; 30 } 31 32 public File[] getFileUpload() { 33 return fileUpload; 34 } 35 36 public void setFileUpload(File[] fileUpload) { 37 this.fileUpload = fileUpload; 38 } 39 40 public String[] getFileUploadFileName() { 41 return fileUploadFileName; 42 } 43 44 public void setFileUploadFileName(String[] fileUploadFileName) { 45 this.fileUploadFileName = fileUploadFileName; 46 } 47 }
(三)文件下载:
1.action层代码:
1 package com.bjsxt.hib.action; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.InputStream; 7 import java.io.UnsupportedEncodingException; 8 9 public class DownloadAction { 10 private InputStream inputStream; 11 private String fileName; 12 13 public String execute(){ 14 try { 15 this.inputStream = new FileInputStream("D:\123.txt");//这里有很多处理手段满足不同需求(从数据库读取等) 16 this.fileName = "哈喽.txt"; 17 } catch (FileNotFoundException e) { 18 e.printStackTrace(); 19 } 20 return "success"; 21 } 22 public InputStream getInputStream() { 23 return inputStream; 24 } 25 public void setInputStream(InputStream inputStream) { 26 this.inputStream = inputStream; 27 } 28 /*乱码处理*/ 29 public String getFileName() { 30 String temp=null; 31 try { 32 temp = new String(this.fileName.getBytes(),"ISO-8859-1"); 33 } catch (UnsupportedEncodingException e) { 34 e.printStackTrace(); 35 } 36 return temp; 37 } 38 public void setFileName(String fileName) { 39 this.fileName = fileName; 40 } 41 42 }
2.struts.xml配置:
1 <action name="downloadAction" class="com.bjsxt.hib.action.DownloadAction"> 2 <result name="success" type="stream"> 3 <!--前两行是不可变的 --> 4 <param name="contentType">application/x-download</param> 5 <param name="inputName">inputStream</param> 6 <param name="contentDisposition">attachment;filename="${fileName}"</param> 7 <param name="bufferSize">4096</param> 8 </result> 9 </action>
3.浏览器直接输入:http://localhost:8080/downloadAction即可访问,下载.