JiveJdon Community Forums
在线464人   首页   主题表   培训咨询   标签   精华   查搜   注册    登陆 RSS
首页 » 论坛 » 项目工程开发经验谈
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表
???en_US.forumThreadNext.name??? 下一主题
Go 总共有 3 回复 / 1
 发表新帖子   回复该主题贴
goddie

悄悄话
发表文章: 47
注册时间: 2007年12月12日 12:10
banq 急!!! org.apache.commons.fileupload 问题 2008年03月03日 00:18 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
标签列表
我是Struts+spring环境

用org.apache.commons.fileupload做一个上传图片的程序,但是不成功:

List<FileItem> items = upload.parseRequest(request);

结果items一直是null,请问用过的人是什么原因。

request经断点调试是有图片的。

上传类在这里:
------------------------------------

package goddie.sns.util;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUpload {

@SuppressWarnings("unchecked")
public static String faceUpload(HttpServletRequest request) {

DiskFileItemFactory factory = new DiskFileItemFactory();

factory.setSizeThreshold(5000);
factory.setRepository(new File(request.getContextPath() + "\\tmp"));

ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(500 * 1000);

try {
if (!ServletFileUpload.isMultipartContent(request)) {
return null;
}

List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {

FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
File uploadedFile = new File(request.getContextPath()
+ "\\1\\abc" + item.getContentType());
try {
item.write(uploadedFile);
return item.getName();
} catch (Exception e) {

e.printStackTrace();
}
}
}

} catch (FileUploadException e) {

e.printStackTrace();
}

return null;

}
}
---------------------------------
[该贴被goddie于2008-03-03 01:03修改过]
[该贴被goddie于2008-03-03 01:04修改过]
goddie

悄悄话
发表文章: 47
注册时间: 2007年12月12日 12:10
re:banq 急!!! org.apache.commons.fileupload 问题 2008年03月03日 08:22 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
看到Jakarta的一个FAQ:

Q:I'm using FileUpload in an Action, but it's not working. Why?

A:Struts recognises multipart requests, and parses them automatically, presenting the request parameters to your code in the same manner as if they were regular request parameters. Since Struts has already processed the request, and made it available in your form bean, the input stream is no longer available for parsing, so attempting to do so with FileUpload will fail.

Q:But I need to parse the request myself. How can I do that?

A:Struts parses multipart a request as a part of the process of populating your form bean from that request. If, for some reason, you need to have full control over the multipart parsing, you can do so by configuring your action mapping without an associated form bean. (A better way of doing this, however, is to replace the default multipart handler with your own. See the Struts documentation for details.)

请问怎么 replace the default multipart handler with your own
[该贴被goddie于2008-03-03 08:22修改过]
banq

悄悄话
发表文章: 9482
注册时间: 2002年08月03日 17:08
回复:banq 急!!! org.apache.commons.fileupload 问题 2008年03月03日 09:13 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
既然使用Struts,就直接使用Struts的上传功能,方便快捷,不要基于request操作了。
goddie

悄悄话
发表文章: 47
注册时间: 2007年12月12日 12:10
re:banq 急!!! org.apache.commons.fileupload 问题 2008年03月03日 10:38 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
恩,看到了。非常简单啊。

请问有没有好的生成缩略图的方案? imageio 生成100象素以内的缩略图非常粗糙
这个主题有 3 回复 / 1Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache 缓存 DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Seam Spring Struts
正在读取,请等待...
google yahoo 新浪ViVi 365Key网摘 天极网摘 CSDN网摘 添加到百度搜藏 POCO网摘 博采网摘
查询本论坛内 回复超过的热门帖子
     回复该主题贴
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
  发贴前查询 标签列表勿重复发表问题

RSS 手机阅读 add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-08 jdon.com
anti spam