SSH框架下使用Commons FileUpload控制元件完成多檔案上傳與下載
阿新 • • 發佈:2019-02-15
某些系統中,經常需要用到上傳與下載功能,其中,如果指定了不同角色上傳多不同檔案,並且指定給其他角色進行下載。那麼一般最好的方法就是將檔案按照不同路徑上傳到不同的資料夾中,然後再在資料庫中存入檔案路徑。
比如在教務系統中,學生要求上傳自己的作業,然後給教師進行下載。其中不同的學生上傳不同的作業檔案,然後老師按照需要下載某個學生的作業檔案。如上,如果直接將檔案上傳到某個資料夾之後,就必須在action或者jsp連結中指定老師需要下載的某個學生作業檔案,這樣就顯得麻煩而且很難實現。 有兩種思路,一種是將檔案轉換為二進位制,然後存入到資料庫中,這種方法有個問題,就是如果上傳的檔案較大超過了限制的大小,那麼就無法上傳到資料庫。 第二種是在專案下或者伺服器中新建一個資料夾,將學生上傳的檔案上傳到這個資料夾中,然後在action中獲取該檔案的路徑以及檔名稱,然後將檔案路徑和檔名稱一起存到資料庫中,因為存入的只是一個String字串,所以檔案大小將沒有被很大程度的限制。 在下載業務中,只要select出相應學生的欄位,然後在jsp頁面寫一個<a>連結就可以指定到某個學生的作業進行下載了。 下面給出相應程式碼。 上傳Action
package com.edu.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.edu.po.Student;
import com.edu.service.StudentService;
import com.opensymphony.xwork2.ActionSupport;
public class StudentUploadFilesAction extends ActionSupport{
//設定資料庫儲存路徑
private String s_path;
//設定學生欄位編號
private String s_no;
//設定檔名
private String title;
//設定檔案
private File upload;
//設定上傳檔案型別
//private String uploadContentType;
//設定上網檔名稱
private String uploadFileName;
//接受依賴注入的屬性
private String savePath;
//業務邏輯元件
private StudentService studentService;
//設定業務邏輯元件的方法
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
//接受依賴注入的方法
public void setSavePath(String value) {
this.savePath = value;
}
private String getSavePath() throws Exception {
return ServletActionContext.getRequest().getRealPath(savePath);
}
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 getS_path() {
return s_path;
}
public void setS_path(String sPath) {
s_path = sPath;
}
public String getS_no() {
return s_no;
}
public void setS_no(String s_no) {
this.s_no = s_no;
}
public String execute() throws Exception {
Student s=studentService.queryStudentScheduledByID(s_no);
s.setS_path("StudentUpload/"+getUploadFileName());
studentService.updateStudentScheduled(s);
// 以伺服器的檔案儲存地址和原檔名建立上傳檔案輸出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "//"+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
//關閉輸入輸出流
fis.close();
fos.close();
return SUCCESS;
}
}
上傳jsp
<body>
<s:iterator value="#request.name" id="student">
你好,<s:property value="#student.s_name"/>
<hr>
<center>
<h1>學生上傳檔案</h1>
<form action="studentuploadfiles.action?s_no=<s:property value="#student.s_no"/>" method="post" enctype="multipart/form-data">
檔案標題:<input type="text" name="title"><br />
選擇檔案:<input type="file" name="upload"><br />
<input type="submit" value="上傳">
</form>
</center>
<hr>
</s:iterator>
</body>
在spring配置文件配置好以來注入,然後在sturts配置文件配置相應Action,這裡使用攔截器規定了檔案大小以及上傳路徑
<!-- studentuploadfiles.action 學生上傳檔案 -->
<action name="studentuploadfiles" class="studentUploadFilesAction">
<interceptor-ref name="fileUpload">
<param name="maximumSize">1024*1024</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<!-- 儲存路徑savePath依賴注入 -->
<param name="savePath">/StudentUpload</param>
<result name="input"> /StudentUploadFiles.jsp</result>
<result name="success">/StudentUploadFilesSuccess.jsp</result>
</action>
下載jsp
下載jsp比較簡單,使用連結下載檔案的方法,在action中獲取了學生資訊,然後傳遞給List物件,再iterator出來,這樣子就可以下載指定的學生檔案了。
<body>
<center>
<h2>學生作業列表</h2>
<table border="1">
<tr>
<td>學生姓名</td>
<td>學生所屬學院</td>
<td>學生所屬系別</td>
<td>學生作業</td>
</tr>
<s:iterator value="#request.dow" id="file">
<tr>
<td><s:property value="#file.s_name"/></td>
<td><s:property value="#file.s_college"/></td>
<td><s:property value="#file.s_department"/></td>
<td><a href="<s:property value="#file.s_path"/>">下載學生作業</a></td>
</tr>
</s:iterator>
</table>
</center>
</body>