struts文件上傳與下載
A. jsp如何實現文件上傳與下載
如果伺服器端程序使用的是struts2框架的話,我會,其他的不會。
struts2:
對於上傳,jsp頁面只需要有個file類型的表單域,如<input type="file" name="xxx" />
struts2的接收請求的action中再寫三個屬性與這個表單域的名稱對應起來,他們是,File類型的xxx,String類型的xxxFileName,String類型的xxxContentType,並其設置相應的set/get方法。則框架負責接收上傳文件的位元組流,解析文件名,文件類型,直接使用即可。
對於下載,只需要在action的配置文件中設置如下返回值類型和相應參數:
<result type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=xxx </param> xxx為下載文件的文件名
</result>
且在action總寫一個返回值類型為InputStream的getInputStream方法,此方法返回你要下載的文件的流即可。
ps:其中contentDisposition的配置信息中attachment代表點擊下載時瀏覽器先彈出個保存位置的提示框,然後再決定是否下載,默認是inline,即直接打開文件。
B. dorado7 文件上傳和下載功能實現 dorado7.0+struts+spring+hibenate框架 希望提供完整的代碼
1. 在dorado studio的Mapping中新建一個Controller. 例如:
<controller name="file" clazz="sample.file.SampleController">
<action name="upload">
<forward name="success" path="/file/upload-success.jsp" contextRelative="false" />
<exception clazz="java.lang.Exception" path="/file/upload-failure.jsp" />
</action>
</controller>
2. 為該Controller新建一個Java實現類. 注意在新建Java類的向導中不必勾選任何待重載的父類方法.
3. 完成上述步驟後. 首先將實現類的父類改為FileController. 然後根據需要重載部分父類中的方法. 可以參考下面的例子:
1package sample.file;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.InputStream;
6import javax.servlet.http.HttpServletRequest;
7
8import org.apache.commons.fileupload.DiskFileUpload;
9import org.apache.commons.fileupload.FileItem;
10import com.bstek.dorado.biz.FileController;
11import com.bstek.dorado.utils.*;
12
13/**
14 * SampleController
15 */
16public class SampleController
17 extends FileController {
18 private final String WORK_DIR = "d:/temp";
19
20 /**
21 * 設定 DiskFileUpload 的相關屬性.
22 * <p>關於DiskFileUpload, 你可以到www.apache.org/commons查找FileUpload的文檔</p>
23 *
24 * @param request HttpServletRequest
25 * @param fileUpload DiskFileUpload
26 * @param parameters MetaData
27 */
28 protected void initFileUpload(HttpServletRequest request,
29 DiskFileUpload fileUpload, MetaData parameters) {
30 fileUpload.setSizeMax(1024 * 512); // 512K
31 }
32
33 /**
34 * 取得存放上傳文件的目標目錄
35 *
36 * @param request HttpServletRequest
37 * @param parameters MetaData
38 * @return String
39 */
40 protected String getUploadWorkDir(HttpServletRequest request,
41 MetaData parameters) {
42 return WORK_DIR;
43 }
44
45 /**
46 * 取得存儲上傳文件的文件名
47 *
48 * @param request HttpServletRequest
49 * @param fileName String
50 * @param parameters MetaData
51 * @return String
52 */
53 protected String getStoreFileName(HttpServletRequest request, String fileName,
54 MetaData parameters) {
55 return fileName;
56 }
57
58 /**
59 * 存儲已經上傳的文件
60 *
61 * @param request HttpServletRequest
62 * @param fileItem String
63 * @param storeFile String
64 * @param parameters MetaData
65 * @throws Exception
66 */
67 protected void storeUploadFile(HttpServletRequest request, FileItem fileItem,
68 File storeFile, MetaData parameters)
69 throws Exception {
70 super.storeUploadFile(request, fileItem, storeFile, parameters);
71
72 /** @todo 在這里您可以添加自己的代碼記錄上傳文件信息 */
73 }
74
75 /**
76 * 取得將要下載的文件的文件名
77 *
78 * @param request HttpServletRequest
79 * @return String
80 */
81 protected String getDownLoadFileName(HttpServletRequest request) {
82 return request.getParameter("fileName");
83 }
84
85 /**
86 * 取得將要被下載的文件的文件輸入流
87 *
88 * @param request HttpServletRequest
89 * @return InputStream
90 * @throws Exception
91 */
92 protected InputStream getDownloadFileInputStream(HttpServletRequest request)
93 throws Exception {
94 return new FileInputStream(WORK_DIR + File.separator +
95 request.getParameter("fileName"));
96 }
97
98}
99
4. 添加一個jsp用來上傳文件. 例如:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page errorPage="/dorado/exception.d" %>
<%@ taglib uri="http://www.bstek.com/dorado" prefix="d" %>
<html>
<head>
<title>文件上傳</title>
</head>
<body>
<center>
<form method="post" target="upload" enctype="multipart/form-data"
action="<%=request.getContextPath()%>/file.upload.d">
<input type="file" name="file1" size="40">
<br>
<input type="file" name="file2" size="40">
<br>
<input type="file" name="file3" size="40">
<hr>
<input type="submit" value=" 上傳 ">
</form>
</center>
</body>
</html>
C. 用Java的三大框架實現文件的上傳下載,求代碼啊,最好是分為action,service,serv
package cn.itcast.struts2.demo1;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 完成文件上傳 (不是解析上傳內容,因為上傳內容 由fileUpload攔截器負責解析)
*
* @author seawind
*
*/
public class UploadAction extends ActionSupport {
// 接收上傳內容
// <input type="file" name="upload" />
private File upload; // 這里變數名 和 頁面表單元素 name 屬性一致
private String uploadContentType;
private String uploadFileName;
public void setUpload(File upload) {
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
@Override
public String execute() throws Exception {
if (upload == null) { // 通過xml配置 required校驗器 完成校驗
// 沒有上傳文件
return NONE;
}
// 將上傳文件 保存到伺服器端
// 源文件 upload
// 目標文件
File destFile = new File(ServletActionContext.getServletContext()
.getRealPath("/upload") + "/" + uploadFileName);
// 文件復制 使用commons-io包 提供 工具類
FileUtils.File(upload, destFile);
return NONE;
}
}
多文件上傳
package cn.itcast.struts2.demo1;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 支持多文件上傳
*
* @author seawind
*
*/
public class MultiUploadAction extends ActionSupport {
// 接收多文件上傳參數,提供數組接收就可以了
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
public void setUpload(File[] upload) {
this.upload = upload;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
@Override
public String execute() throws Exception {
for (int i = 0; i < upload.length; i++) {
// 循環完成上傳
File srcFile = upload[i];
String filename = uploadFileName[i];
// 定義目標文件
File destFile = new File(ServletActionContext.getServletContext()
.getRealPath("/upload" + "/" + filename));
FileUtils.File(srcFile, destFile);
}
return NONE;
}
}
D. struts2文件上傳是什麼流程
1,頁面設置, 一定有個表單,表單一定要設置兩個屬性method和enctype
method是表單提交方式,enctype是表單域內容以流的方式處理
<form method="post"
enctype="multipart/form-data"></form>
2,在表單內添加一個input,type="file",一定要添加一個name屬性
<form method="post" enctype="multipart/form-data">
<input type="file" name="file1">
</form>
3,控制器接值,可以接三個值(文件,文件名,文件類型)
private File file1;
private String file1FileName;
private String file1ContentType;
以上三個變數必須做setget方法,
4,struts2文件上傳用的是fileUploadInterceptor攔截器,默認文件大小為<=2M,類型沒限制
E. struts2文件上傳和下載
1,上傳方法
(1),頁面form表單添加一個屬性為enctype="multipart/form-data" 和method="post"
(2),假設上傳預覽框為 <input type="file" name="myfile" />
(3),控制器接值的方法為
private File myfile; //要上傳的文件
private String myfileFileName; //要上傳文件名稱
private String myfileContentType; //要上傳文件類型
別忘了做set方法
(4), 接到值後可以保存到資料庫,也可以保存到硬碟,
>>1 保存到資料庫, 資料庫表中對應欄位要設置為BLOB類型
>>2 保存到硬碟代碼如下
InputStream in = new
FileInputStream( myfile);
OutputStream out = new
FileOutputStream( new File("d:\\upload\\"+myfileFileName));
byte[] buffer
= new byte[ in.available() ];
int ins =
in.read(buffer);//讀取位元組到buffer中
//ins == -1 時
。就已經是文件的結尾了
while ( ins !=
-1 ) {
out.write(buffer, 0, ins);//將緩存buffer中的數據寫到文件中
ins = in.read(buffer);
}
in.close();
out.flush();
out.close();
2,下載
(1), 把要下載的文件轉成一個輸入流InputStream
例如,利用hibernate取得一個文件,文件類型在實體類中為byte[]類型,
inputStream = new
ByteArrayInputStream(book.getMyfile);
其中inputStream 為全局變數,並且做setter和getter方法
(2),在控制器對應的action節點中(struts2配置文件中)添加一個result節點如下:
<result name="download" type="stream">
<param name="contentType">application/zip</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${myFileFileName}"</param>
<param name="bufferSize">1024</param>
</result>
這樣,就可以實現上傳和下載了.