(一)单个文件的上传步骤:
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即可访问,下载.