1

我有一个 jaxws WebService,它允许 downloadImage(或者至少应该......) webservice 的 cod:

@WebService(name = "SimpleWebService")
@MTOM
@SOAPBinding(style = Style.RPC)
public class SimpleWebService {
@WebMethod
    public Image getImage(String imgName) throws IOException{
        System.out.println("Image name "+imgName);
        File fajl = new File(imgName);
        return ImageIO.read(new File(imgName));
    }
}

我已经生成了客户端。包edu.pdfbookshelf.wsclient;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.activation.DataHandler;
import javax.imageio.ImageIO;

public class Client {

/**
 * @param args
 * @throws IOException_Exception 
 */
public static void main(String[] args) throws IOException_Exception {
     SimpleWebServiceService service = new SimpleWebServiceService();
       SimpleWebService port = service.getSimpleWebServicePort();

       byte[] imageIS = port.getImage("rainbow.jpg");
       ByteArrayOutputStream os = new ByteArrayOutputStream();
       InputStream fis = new ByteArrayInputStream(imageIS);
       DataHandler image = new DataHandler(new IStoDataSource(fis));
       dump(image);


}
 private static void dump(DataHandler dh) {
       System.out.println();
       try {
          System.out.println("MIME type: " + dh.getContentType());
          System.out.println("Content:   " + dh.getContent());
          InputStream in = dh.getInputStream();
          BufferedImage buffferedImage = ImageIO.read(in);
          ImageIO.write(buffferedImage, "jpg", new File("test.jpg"));
               }
           catch(IOException e) { System.err.println(e); }
        }


}

当我尝试运行客户端时,出现异常:

SEVERE: Can't read input file!
javax.imageio.IIOException: Can't read input file!
        at javax.imageio.ImageIO.read(ImageIO.java:1291)
        at edu.pdfbookshelf.ws.SimpleWebService.getImage(SimpleWebService.java:143)
        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:616)
        at com.sun.xml.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:250)
        at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:149)
        at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:88)
        at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1063)
        at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:979)
        at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:950)
        at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:825)
        at com.sun.xml.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:380)
        at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:651)
        at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:264)
        at com.sun.xml.ws.transport.http.servlet.ServletAdapter.invokeAsync(ServletAdapter.java:218)
        at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:159)
        at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:194)
        at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:80)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:679)

我不知道为什么我得到那个例外。文件在根文件夹中。没有文件未找到异常,所以我认为路径很好。有什么想法可能是错的吗?为了创建 WebService 和客户端,我使用了http://ojitha.blogspot.com/2012/02/jax-ws-binary-data-passing-using-mtom.html

4

2 回答 2

2

我认为错误在于

byte[] imageIS = port.getImage("rainbow.jpg");

应该

byte[] imageIS = port.getImage("/rainbow.jpg");

否则将被解释为相对于应用服务器启动目录的路径。

于 2012-12-23T17:20:10.103 回答
0

检查imgName文件的路径是否正确:

log.info("File exists?: " + new File(imgName).exists()); //or System.out.println(...);
log.info(imgName);
log.info(new File(imgName).getAbsolutePath());

如果一切正常 - 那么您的图像可能已损坏。如果您可以在您最喜欢的图像编辑工具中打开此文件 - 重新保存并覆盖它。

于 2012-12-23T16:30:26.977 回答