struts2單文件上傳:
首先是一個jsp文件上傳頁面,這個比較簡單,就是一個表單,裏面有個文件上傳框
<!--在進行文件上傳時,表單提交方式一定要是post的方式,因爲文件上傳時二進制文件可能會很大,還有就是enctype屬性,這個屬性一定要寫成multipart/form-data,
不然就會以二進制文本上傳到服務器端-->
<s:actionerror/><br/>
<!--注意:添加命名空間namespace="/"、否則在第一次上傳之後地址將發生改變-->
<!--注意:2.enctype="multipart/form-data"規定上傳類型 否則上傳文件在那頭會出錯-->
<s:form action="upload" enctype="multipart/form-data" namespace="/" method="POST">
<!--做標記用 用來判斷是否重複登陸-->
<s:token/>
<s:file name="myFile" label="文件上傳"/>
<s:textfield name="name" label="用戶名"/>
<s:submit value="提交"/>
</s:form>
<h2>文件下載內容:</h2><br/>
a.html:<a href="down">點擊下載</a><br/>
<!--struts.xml -->
<package name="my" namespace="/" extends="struts-default">
<action name="upload" class="com.cn.struts.action.MyFileUploadAction">
<result>/success.jsp</result>
</action>
<action name="down" class="com.cn.struts.action.MyDownloadAction">
<result type="stream">
<param name="bufferSize">1024</param>
</result>
</action>
</package>
接下來是FileUploadAction部分代碼,因爲struts2對上傳和下載都提供了很好的實習機制,所以在action這段我們只需要寫很少的代碼就行:
package com.cn.struts.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class MyFileUploadAction extends ActionSupport{
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
private File myFile;// 上傳來的文件
private String myFileContentType;// 上傳文件的類型
private String myFileFileName;// 上傳文件的名字
public String execute() throws Exception {
//1.獲得輸入流
InputStream in=new FileInputStream(myFile);
//2.獲得文件存儲路徑
//tomcat下webapps 項目名
File toDir=new File(ServletActionContext.getServletContext().getRealPath("/upload"));
//3.判斷是文件路徑是否存在,不存在則創建文件
if(toDir.exists()==false){
toDir.mkdir();
}
System.out.println(toDir.getAbsolutePath());
//4.輸出流
OutputStream out=new FileOutputStream(new File(toDir,myFileFileName));
byte[] bs=new byte[1024];
int len=0;
while((len=in.read(bs))!=-1){
out.write(bs,0,len);
}
out.flush();
in.close();
out.close();
return "success";
}
}
首先我們要清楚一點,這裏的myFile並不是真正指代jsp上傳過來的文件,當文件上傳過來時,struts2首先會尋找struts.multipart.saveDir(這個是在default.properties裏面有)這個name所指定的存放位置,我們可以新建一個struts.properties屬性文件來指定這個臨時文件存放位置,如果沒有指定,那麼文件會存放在tomcat的apache-tomcat-7.0.29\work\Catalina\localhost\目錄下,然後我們可以指定文件上傳後的存放位置,通過輸出流將其寫到流裏面就行了,這時我們就可以在文件夾裏看到我們上傳的文件了。
文件上傳後我們還需要將其下載下來,其實struts2的文件下載原理很簡單,就是定義一個輸入流,然後將文件寫到輸入流裏面就行,關鍵配置還是在struts.xml這個配置文件裏配置:
FileDownloadAction代碼如下:
package com.cn.struts.action;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class MyDownloadAction extends ActionSupport{
protected String contentType;//下載內容類型
protected long contentLength;//下載內容長度
protected String contentDisposition;//下載內容配置(名字)
protected InputStream inputStream;//輸入流
public String execute() throws Exception{
contentType="text/html";
//contentDisposition="attachment;filename="+new String("你好.html".getBytes(), "iso-8859-1");
contentDisposition="attachment;filename="+URLEncoder.encode("你好.html", "iso-8859-1");//轉碼 firebug不會識別這個utf-8
String downloadPath=ServletActionContext.getServletContext().getRealPath("/upload/a.html");
inputStream=new FileInputStream(downloadPath);
contentLength=inputStream.available();
return "success";
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public long getContentLength() {
return contentLength;
}
public void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
public String getContentDisposition() {
return contentDisposition;
}
public void setContentDisposition(String contentDisposition) {
this.contentDisposition = contentDisposition;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
}