当前位置: 代码迷 >> J2SE >> 用servlet生成一个图少时报错
  详细解决方案

用servlet生成一个图少时报错

热度:137   发布时间:2016-04-24 12:43:50.0
用servlet生成一个图片时报错
以下是servlet的代码:
---------------------------------------------------------------
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.net.URL;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.*;

public class CreateImageServlet extends HttpServlet {

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException
  {
  response.setContentType("image/jpeg");
  /*
  * public ServletOutputStream getOutputStream() :
  * 返回一个适合在响应中写入二进制数据的ServletOutputStream类
  * public abstract class ServletOutputStream():
  * 提供一个向客户端发送二进制数据的输出流
  */
  createImage(response.getOutputStream());
  }
   
  //生成图像的方法
  private void createImage(OutputStream out) throws IOException{

  BufferedImage bi2 = ImageIO.read(new URL("D:\\Documents and Settings\\My Pictures\\piao.jpg"));

   
  // encode:
  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi2);
  param.setQuality(1.0f, false);
  encoder.setJPEGEncodeParam(param);
  try {
  encoder.encode(bi2);
  }
  catch(IOException ioe) {
  ioe.printStackTrace();
  }
  }
}
---------------------------------------------------------------
以下是报错内容:
Servlet.service() for servlet CreateImageServlet threw exception
java.net.MalformedURLException: unknown protocol: d
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at image_edit.CreateImageServlet.createImage(CreateImageServlet.java:50)
at image_edit.CreateImageServlet.doGet(CreateImageServlet.java:25)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
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:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)


------解决方案--------------------
http://blogger.org.cn/dispbbs.asp?boardID=2&ID=31881
  相关解决方案