浏览 4421 次
|
锁定老贴子 主题:如何使用webwork 上传文件到不同目录
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2005-06-01
在研究webwork上传,发现在ServletDispatcher中构造了 MultiPartRequestWrapper 而且路径是唯一的。这样就表示我无法实现将文件上传到其他目录下。
[code:1] protected HttpServletRequest wrapRequest(HttpServletRequest request) throws IOException { // don't wrap more than once if (request instanceof MultiPartRequestWrapper) { return request; } if (MultiPartRequest.isMultiPart(request)) { request = new MultiPartRequestWrapper(request, getSaveDir(), getMaxSize()); } return request; } [/code:1] 我尝试自己去写一个interceptor 去上传,可是servletDispatcher 已经把 MultiPartRequestWrapper构造好了,我不知道该怎么去做,尝试了好几种方法都不行。哪位如果知道的话,请告诉我一下,谢谢。 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2005-06-01
你需要自己把对应的File保存到自己需要的目录下或者数据库或者....
webwork的interceptor可以帮你把文件映射到action里面,看看webwork的例子 |
|
| 返回顶楼 | |
|
最后更新时间:2005-06-01
在你的action里面定义一个 java.io.File类型的变量,名字对应你的页面中的HTML标记<input type="file" name="XXX" />,这样你在Action中就可以直接通过这个File对象进行文件内容的读取了。(记得给你的这个action配置FileUploadInterceptor)
Webwork的FileUploadInterceptor是把上传的文件放到一个临时目录里面去,然后在Action里面给File对象赋好值,让你通过这个File对象直接访问这个文件。 但是请注意,你应该在action中根据自己的业务需要进行相应的处理(例如把文件内容读取出来,放在数据库blob中;例如拷贝到应用程序定义的某个目录下面等等),Action执行完毕后,FileUploadInterceptor会删除临时目录中的上传文件。 还要注意的是,你的FileUploadInterceptor应该配置到ParametersInterceptor前面。 BTW:其实webwork的FileuploadInterceptor做的并不好,为了统一封装不同的文件上传库,丧失了很多功能。 |
|
| 返回顶楼 | |
|
最后更新时间:2005-06-01
robbin 写道 在你的action里面定义一个 java.io.File类型的变量,名字对应你的页面中的HTML标记<input type="file" name="XXX" />,这样你在Action中就可以直接通过这个File对象进行文件内容的读取了。(记得给你的这个action配置FileUploadInterceptor)
Webwork的FileUploadInterceptor是把上传的文件放到一个临时目录里面去,然后在Action里面给File对象赋好值,让你通过这个File对象直接访问这个文件。 但是请注意,你应该在action中根据自己的业务需要进行相应的处理(例如把文件内容读取出来,放在数据库blob中;例如拷贝到应用程序定义的某个目录下面等等),Action执行完毕后,FileUploadInterceptor会删除临时目录中的上传文件。 还要注意的是,你的FileUploadInterceptor应该配置到ParametersInterceptor前面。 BTW:其实webwork的FileuploadInterceptor做的并不好,为了统一封装不同的文件上传库,丧失了很多功能。 谢谢。明白了,原来它只是为了创建个临时文件,怪不得在interceptor里面call action 执行。 |
|
| 返回顶楼 | |
|
最后更新时间:2005-06-04
我作过好象是这么写file.renameto(路径)
|
|
| 返回顶楼 | |
|
最后更新时间:2006-08-31
[code:1]
package com.crazyasp.function; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Random; import javax.servlet.http.HttpSession; import com.crazyasp.common.DealAction; import com.opensymphony.webwork.ServletActionContext; /** * TODO : 完成图片的上传操作,利用webwork上传,这里接受并拷贝即可 * @author : davy * date : 2006-03-13 * @copyright : 1.0 */ public class Upload extends DealAction{ private static final long serialVersionUID = 1L; //图片框的名字,所以调用的页面上的图片框要用"name = picture" private File picture = null; //上传文件的真实文件名 private String fileName; //图片上传的路径,应该是绝对路径,通过在页面上增加一个隐藏的html对象指定它 //即<input type="hidden" name="path" value="D:\\Program Files\\Tomcat\\webapps\\ROOT\\uploadFiles\\"> private String path = ""; //与action相对应的方法 public String upLoadPicture() throws Exception { String result = ERROR; if(picture != null) { Random r = new Random(); int i = r.nextInt(9999);//生成4位随机数 //生成图片新文件名 String extName = ""; int start = picture.getName().indexOf("."); if(start > 0) { extName = picture.getName().substring(start+1); } String newPath = StringToDate.getCurrentDate4Random()+i+"."+extName; //this.path = "D:\\Program Files\\Tomcat\\webapps\\ROOT\\uploadFiles\\"; String url = this.path + newPath; if(copyFile(picture.getAbsoluteFile().toString(),url).equals("ok")) { result = SUCCESS; //Map session = new HashMap(); //session.put("pictureName",url); HttpSession session = ServletActionContext. getRequest().getSession(); session.putValue("pictureName",newPath); log.debug("------>session:" + newPath); //ActionContext.getContext().setSession(session); }else { result = ERROR; } } return result; } /** * TODO 这个方法是给外部调用的,返回图片的名称 * @author Davy.Lee * @return 图片名称,string */ public String upLoad() throws Exception { log.debug("realName:--->" + this.fileName); log.debug("realName2:-->" + this.getPictureFileName()); log.debug("picturegetName:-->" + picture.getName()); log.debug("toString:-->" + picture.toString()); String result = ERROR; log.debug("picture--->" + picture); if(picture != null) { Random r = new Random(); int i = r.nextInt(9999);//生成4位随机数 //生成图片新文件名 String extName = ""; int start = picture.getName().indexOf("."); if(start > 0) { extName = picture.getName().substring(start+1); } String newPath = StringToDate.getCurrentDate4Random()+i+"."+extName; String url = this.path + newPath; log.debug("--->picture.getAbsoluteFile():" + picture.getAbsoluteFile().toString()); log.debug("--->url" + url); if(copyFile(picture.getAbsoluteFile().toString(),url).equals("ok")) { result = newPath; }else { result = ERROR; } } return result; } /** * 复制单个文件 * @param oldPath String 原文件路径 如:c:/fqf.txt * @param newPath String 复制后路径 如:f:/fqf.txt * @return boolean * @author Davy Lee * date 2006-03-13 23:38 */ public String copyFile(String oldPath, String newPath) { String result = ""; try { log.debug("************************oldpath="+oldPath); log.debug("************************newpath="+newPath); File newFile = new File(newPath); if(!newFile.exists()) { newFile.createNewFile(); } int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if(oldfile.exists()) { //文件存在时 InputStream inStream = new FileInputStream(oldPath); //读入原文件 FileOutputStream fs = new FileOutputStream(newFile); byte[] buffer = new byte[1444]; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //字节数 文件大小 System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); result = "ok"; } } catch (Exception e) { log.debug("复制单个文件操作出错"); e.printStackTrace(); result = "no"; } return result; } /** * TODO 设置上传的文件的真实文件名 * @author davy lee * date 2006-08-30 */ public void setPictureFileName(String fileName) { this.fileName = fileName; } public String getPictureFileName() { return this.fileName; } /** * @return Returns the path. */ public String getPath() { return path; } /** * @param path The path to set. */ public void setPath(String path) { this.path = path; } /** * @return Returns the picture. */ public File getPicture() { return this.picture; } /** * @param picture The picture to set. */ public void setPicture(File picture) { this.picture = picture; } /** * @return Returns the fileName. */ public String getFileName() { return fileName; } /** * @param fileName The fileName to set. */ public void setFileName(String fileName) { this.fileName = fileName; } } [/code:1] |
|
| 返回顶楼 | |
|
最后更新时间:2006-08-31
我无论如何也得不到上传的文件的文件名,当然我关心的只是其扩展名,因为主文件名我重新按日期时间生成的。
|
|
| 返回顶楼 | |
|
最后更新时间:2006-09-05
jfy3d 写道 我作过好象是这么写file.renameto(路径)
这个好像不难道吧....所引用 的这位楼上的正解啊..我就是这样写的..没有问题.帮他详细一下. file.renameto(new File(路径+文件名)) |
|
| 返回顶楼 | |









