banq 急!!! org.apache.commons.fileupload 问题

08-03-03 goddie
我是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
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
2008-03-03 09:13
既然使用struts,就直接使用Struts的上传功能,方便快捷,不要基于request操作了。

goddie
2008-03-03 10:38
恩,看到了。非常简单啊。

请问有没有好的生成缩略图的方案? imageio 生成100象素以内的缩略图非常粗糙

猜你喜欢