<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
17extends 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,
29DiskFileUpload fileUpload, MetaData parameters) {
30fileUpload.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,
41MetaData parameters) {
42return 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,
54MetaData parameters) {
55return 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 {
70super.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) {
82return 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 {
94return new FileInputStream(WORK_DIR + File.separator +
95 request.getParameter("fileName"))
96 }
97
98}
99
4. 添加一个jsp用来上传文件. 例如:
<%@ page contentType="text/htmlcharset=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>
#region 上传图片方法/// <summary>
/// 上传图片方法
/// </summary>
/// <param name="linkImg">FileUpload控件</param>
/// <param name="links">预览图片img的名字</param>
/// <param name="file">上传图片的路径</param>
/// <returns></returns>
public string AddImg(FileUpload linkImg, HtmlImage links, string file)
{
string img = ""
if (linkImg.HasFile)//检查是否有文件
{
string fullFileName = linkImg.PostedFile.FileName //文件路径名
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1) /闭败/图片名称
string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1) //图片格式
if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp" || type == "BNG" || type == "png") //判断是否为图片类型
{
string path = HttpContext.Current.Request.MapPath("~/" + file + "/")//获取上传文件的网站目录路轿含颤老圆径
linkImg.SaveAs(path + fileName)//存储文件到磁盘
if (links != null)
{
links.Src ="~/" + file + "/" + fileName//显示图片
}
img ="~/"+ file + "/" + fileName
}
else
{
HttpContext.Current.Response.Write("<script>alert('非图片类型,不允许上传!')</script>")
}
}
else
{
HttpContext.Current.Response.Write("<script>alert('必须指定文件!')</script>")
}
return img
}
#endregion
/// <summary>
/// 添加Flash按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAdd_Click(object sender, EventArgs e)
{
Flashs flash = new Flashs()
flash.FName = LinkURL.Text.Trim().ToString()
string url = this.AddImg(ImgURL, pic, "upload")
flash.FUrl = url
int result = FlashManager.AddFlash(flash)
if (result >0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功!')</script>")
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败!')</script>")
}
}
这些都是从项目里面拷贝出来的,可以直接使用的。有什么问题,再找糊涂。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)