无法读取文件异常

时间:2012-12-23 15:38:59

标签: java web-services jax-ws javax.imageio

我有一个允许下载图像的jaxws WebService(或至少应该...) 网络服务的鳕鱼:

@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)

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

2 个答案:

答案 0 :(得分:2)

我认为错误在

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

应该是

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

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

答案 1 :(得分:0)

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

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

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