Apache wink で REST Service を使用してファイルをアップロードしたいと考えています。
HTTP クライアントを使用しています。
org.apache.wink.client.ClientConfig clientConfig = new org.apache.wink.client.ClientConfig();
javax.ws.rs.core.Application app = new javax.ws.rs.core.Application() {
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(RestResource.class);
return classes;
}
};
clientConfig.applications(app);
org.apache.wink.client.RestClient client = new org.apache.wink.client.RestClient(clientConfig);
org.apache.wink.client.Resource resourceCreateProductImage = client.resource("http://restResource/uploadProductImage");
File file = new File("/home/aspire17/Pictures/Product.png") ;
String fileName = file.getName();
MultipartEntity multiPartEntity = new MultipartEntity();
multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName())) ;
FileBody fileBody = new FileBody(file, "application/octect-stream") ;
multiPartEntity.addPart("attachment", fileBody) ;
ClientResponse responseCreateProductImage = resourceCreateProductImage
.contentType("multipart/form-data").accept("*/*").post("");
私のApacheウィンクレストサービスは、
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("/uploadProductImage")
public JSONObject uploadProductImage(BufferedInMultiPart bimp)
throws IOException, JSONException {
OutputStream out = null ;
Random rand = new Random();
List parts = bimp.getParts();
Debug.logInfo("parts size : " + parts.size(), module);
Iterator it = parts.iterator();
byte[] bytes = null;
while(it.hasNext()){
InPart name = (InPart) it.next();
try{
InputStream inputStream= name.getInputStream();
out = new FileOutputStream("/home/aspire17/Pictures/Product.png");
int read=0;
bytes = new byte[1024];
while((read = inputStream.read(bytes))!= -1){
out.write(bytes, 0, read);
}
inputStream.close();
out.flush();
out.close();
}
catch (IOException e){
}
}
}
Rest サービスでファイルを取得する方法がわかりません
現在、このサービスを実行していると、例外を下回っています
[java] 2013-05-29 18:59:14,784 (http-bio-0.0.0.0-8080-exec-2)[StandardWrapperValve.java:269:ERROR] サーブレット [restServlet] の Servlet.service() とのコンテキストでパス [/restAPI] が例外 [java] java.lang.ArrayIndexOutOfBoundsException [java] at java.lang.System.arraycopy(Native Method) [java] at org.apache.wink.common.internal.providers.multipart.MultiPartParser. shiftBuff(MultiPartParser.java:80) [java] at org.apache.wink.common.internal.providers.multipart.MultiPartParser.fetch(MultiPartParser.java:201) [java] at org.apache.wink.common.internal. providers.multipart.MultiPartParser.verifyByteReadyForRead(MultiPartParser.java:183) [java] org.apache.wink.common.internal.providers.multipart.MultiPartParser.digestBoundary(MultiPartParser.java:144) [java] org.apache で。ウィンク。internal.providers.multipart.MultiPartParser.nextPart(MultiPartParser.java:105) [java] org.apache.wink.common.model.multipart.InMultiPart.hasNext(InMultiPart.java:83) [java] org.apache で。 wink.common.model.multipart.BufferedInMultiPart.(BufferedInMultiPart.java:50) [java] org.apache.wink.common.internal.providers.multipart.BufferedInMultiPartProvider.readFrom(BufferedInMultiPartProvider.java:62) [java] org .apache.wink.common.internal.providers.multipart.BufferedInMultiPartProvider.readFrom(BufferedInMultiPartProvider.java:39) [java] org.apache.wink.server.internal.registry.ServerInjectableFactory$EntityParam.getValue(ServerInjectableFactory.java:200) ) [java] at org.apache.wink.common.internal.registry.InjectableFactory.instantiate(InjectableFactory.java:68) [java] at org.apache.wink.server.internal.handlers.CreateInvocationParametersHandler.handleRequest(CreateInvocationParametersHandler.java:44) [java] org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) [java] org. apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) [java] at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) [java] at org.apache. wink.server.internal.handlers.FindResourceMethodHandler.handleSubResourceMethod(FindResourceMethodHandler.java:183) [java] org.apache.wink.server.internal.handlers.FindResourceMethodHandler.handleRequest(FindResourceMethodHandler.java:110) [java] org. apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) [java] at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) [java] at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) [java] org.apache.wink.server.internal.handlers.FindRootResourceHandler.handleRequest(FindRootResourceHandler.java:95) で [java] org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) で[java] org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) で [java] org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) で [java ] org.apache.wink.server.internal.handlers.HeadMethodHandler.handleRequest(HeadMethodHandler.java:53) で [java] org.apache.wink.server.handlers で。RequestHandlersChain.handle(RequestHandlersChain.java:26) [java] at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) [java] at org.apache.wink.server.handlers.AbstractHandlersChain. doChain(AbstractHandlersChain.java:63) [java] at org.apache.wink.server.internal.handlers.OptionsMethodHandler.handleRequest(OptionsMethodHandler.java:46) [java] at org.apache.wink.server.handlers.RequestHandlersChain.ハンドル (RequestHandlersChain.java:26) [java] at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) [java] at org.apache.wink.server.handlers.AbstractHandlersChain.doChain( AbstractHandlersChain.java:63) [java] org.apache.wink.server.internal.handlers.SearchResultHandler.handleRequest(SearchResultHandler.java:33) [java] at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) [java] at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) [java] org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) で [java] org.apache.wink.server.internal.log.ResourceInvocation.handleRequest(ResourceInvocation.java:92) で[java] org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) で [java] org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) で [java ] org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) で [Java] org.apache.wink.server.internal.log.Requests で。handleRequest(Requests.java:76) [java] at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) [java] at org.apache.wink.server.handlers.RequestHandlersChain.handle( RequestHandlersChain.java:22) [java] at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63) [java] at org.apache.wink.server.handlers.AbstractHandlersChain.run(AbstractHandlersChain. java:48) [java] org.apache.wink.server.internal.RequestProcessor.handleRequestWithoutFaultBarrier(RequestProcessor.java:207) で [java] org.apache.wink.server.internal.RequestProcessor.handleRequest(RequestProcessor.java: 154) [java] at org.apache.wink.server.internal.servlet.RestServlet.service(RestServlet.java:119) [java] at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) [java] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [java] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java: 210) [java] at org.ofbiz.webapp.control.ContextFilter.doFilter(ContextFilter.java:337) [java] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [java] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [java] org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [java] org.apache.catalina. core.StandardContextValve.invoke(StandardContextValve.java:123) [java] org.apache.catalina.authenticator.AuthenticatorBase.(AuthenticatorBase.java:472) [java] org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) [java] org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java: 99) [java] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [java] at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) [java] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) [java] at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002) [java] at org.apache.coyote. AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) [java] org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) [java] at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [java] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [java] at java.lang.Thread.run(Thread.java:619)