我想从webform检索通过方法发布的文件,我使用此代码:
@Post
public Representation accept(Representation entity) throws FileUploadException, IOException {
Representation result = null;
if (entity != null
&& MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(),
true)) {
// 1/ Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1000240);
// 2/ Create a new file upload handler based on the Restlet
// FileUpload extension that will parse Restlet requests and
// generates FileItems.
RestletFileUpload upload = new RestletFileUpload(factory);
// 3/ Request is parsed by the handler which generates a
// list of FileItems
//System.out.println(entity.getSize());
FileItemIterator fileIt = upload.getItemIterator(entity);
// Process only the uploaded item called "fileToUpload"
// and return back
boolean found = false;
while (fileIt.hasNext() && !found) {
FileItemStream fi = fileIt.next();
if (fi.getFieldName().equals("fileToUpload")) {
// For the matter of sample code, it filters the multo
// part form according to the field name.
found = true;
// consume the stream immediately, otherwise the stream
// will be closed.
StringBuilder sb = new StringBuilder("media type: ");
sb.append(fi.getContentType()).append("\n");
sb.append("file name : ");
sb.append(fi.getName()).append("\n");
BufferedReader br = new BufferedReader(new InputStreamReader(fi.openStream()));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
sb.append("\n");
result = new StringRepresentation(sb.toString(),
MediaType.TEXT_PLAIN);
}
}
} else {
// POST request with no entity.
setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
}
return result;
}
当我发送请求时:
POST /upload HTTP/1.1
Host: localhost:8023
Content-Type: multipart/form-data;boundary=------FormBoundary14c70e575bf
Content-Length: 28223
------FormBoundary14c70e575bf
Content-Disposition: form-data; name="fileToUpload"; filename="fileToUpload"
Content-Type: application/octet-stream
[message-part-body; type:application/octet-stream, size:28037 bytes]
------FormBoundary14c70e575bf--
当我想构建迭代器时,问题就出现了:
FileItemIterator fileIt = upload.getItemIterator(entity);
mars 31, 2015 7:33:49 PM org.restlet.resource.UniformResource doCatch
AVERTISSEMENT: Exception or error caught in resource
java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:123)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:70)
at org.apache.commons.fileupload.MultipartStream.readBodyData(MultipartStream.java:593)
at org.apache.commons.fileupload.MultipartStream.discardBodyData(MultipartStream.java:617)
at org.apache.commons.fileupload.MultipartStream.skipPreamble(MultipartStream.java:634)
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:1023)
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:1003)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310)
at org.restlet.ext.fileupload.RestletFileUpload.getItemIterator(RestletFileUpload.java:97)
at fileupload.FileUploadServerResource.accept(FileUploadServerResource.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:449)
at org.restlet.resource.ServerResource.post(ServerResource.java:1114)
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:533)
at org.restlet.resource.ServerResource.doNegotiatedHandle(ServerResource.java:590)
at org.restlet.resource.ServerResource.doConditionalHandle(ServerResource.java:302)
at org.restlet.resource.ServerResource.handle(ServerResource.java:849)
at org.restlet.resource.Finder.handle(Finder.java:513)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Router.doHandle(Router.java:500)
at org.restlet.routing.Router.handle(Router.java:740)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:155)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.engine.ChainHelper.handle(ChainHelper.java:114)
at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:75)
at org.restlet.Application.handle(Application.java:391)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Router.doHandle(Router.java:500)
at org.restlet.routing.Router.handle(Router.java:740)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Router.doHandle(Router.java:500)
at org.restlet.routing.Router.handle(Router.java:740)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:155)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.engine.ChainHelper.handle(ChainHelper.java:114)
at org.restlet.Component.handle(Component.java:391)
at org.restlet.Server.handle(Server.java:491)
at org.restlet.engine.http.connector.BaseServerHelper.handle(BaseServerHelper.java:161)
at org.restlet.engine.http.connector.BaseServerHelper.handleInbound(BaseServerHelper.java:170)
at org.restlet.engine.http.connector.BaseHelper.handleNextInbound(BaseHelper.java:421)
at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:698)
at org.restlet.engine.http.connector.Controller$2.run(Controller.java:98)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:0)
我认为您需要在应用程序的类路径中添加Apache Commons IO库的jar文件。也就是说,你应该使用Maven来配置和解决你的依赖关系,因为它会为你处理这个问题。
以下是在Restlet中配置上传支持的示例内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.restlet</groupId>
<artifactId>restlet-upload-sample</artifactId>
<name>${project.artifactId}</name>
<packaging>jar</packaging> <!-- or war -->
<version>1.0.0-snapshot</version>
<properties>
<restlet-version>2.3.1</restlet-version>
</properties>
<dependencies>
<dependency>
<groupId>org.restlet.jse</groupId> <!-- or org.restlet.jee, ... -->
<artifactId>org.restlet</artifactId>
<version>${restlet-version}</version>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.fileupload</artifactId>
<version>${restlet-version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.com</url>
</repository>
</repositories>
</project>
希望它可以帮到你, 亨利