1. 在dorado studio的Mapping中新建一个Controller. 例如:
2. 为该Controller新建一个Java实现类. 注意在新建Java类的向导中不必勾选任何待重载的父类方法.
3. 完成上述步骤后. 首先将实现类的父类改为FileController. 然后根据需要重载部分父类中的方法. 可以参考下面的例子:
4. 添加一个jsp用来上传文件. 例如:
5. 如果要下载Server端的文件只要使用通过url访问我们新定义的FileController就可以了. 例如:
http://localhost:8080/xxxx/file.download.d?filename=文件名
<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>
<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. 然后根据需要重载部分父类中的方法. 可以参考下面的例子:
1
package sample.file;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.InputStream;
6 import javax.servlet.http.HttpServletRequest;
7
8 import org.apache.commons.fileupload.DiskFileUpload;
9 import org.apache.commons.fileupload.FileItem;
10 import 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
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.InputStream;
6 import javax.servlet.http.HttpServletRequest;
7
8 import org.apache.commons.fileupload.DiskFileUpload;
9 import org.apache.commons.fileupload.FileItem;
10 import 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>
<%@ 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>
5. 如果要下载Server端的文件只要使用通过url访问我们新定义的FileController就可以了. 例如:
http://localhost:8080/xxxx/file.download.d?filename=文件名