当前位置: 技术问答>java相关
哪位(大大...)哥知道怎么用servlet(resin)传送图片?
来源: 互联网 发布时间:2015-02-01
本文导语: 我用servlet(resin)编写一个传送图片的程序。代码如下: static String basePath="c:\images"; static String pict="image1.jpg"; .... public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExcepti...
我用servlet(resin)编写一个传送图片的程序。代码如下:
static String basePath="c:\images";
static String pict="image1.jpg";
....
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
File f=new File(basePath,pict);
int size=0;
byte[] buf;
try
...
String type="image/gif";
resp.setContentType(type);
resp.setContentLength(size);
OutputStream out=resp.getOutputStream();
RandomAccessFile raf=new RandomAccessFile(f,"r");
raf.read(buf);
out.close();
}
catch(Exception ex)
{System.err.println("imageServlet throws: "+ex);
ex.printStackTrace(System.err);
}
图形总是出不来,亲吻:那里出问题了?
static String basePath="c:\images";
static String pict="image1.jpg";
....
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
File f=new File(basePath,pict);
int size=0;
byte[] buf;
try
...
String type="image/gif";
resp.setContentType(type);
resp.setContentLength(size);
OutputStream out=resp.getOutputStream();
RandomAccessFile raf=new RandomAccessFile(f,"r");
raf.read(buf);
out.close();
}
catch(Exception ex)
{System.err.println("imageServlet throws: "+ex);
ex.printStackTrace(System.err);
}
图形总是出不来,亲吻:那里出问题了?
|
如果没有抛出异常,说明文件是找到了的,也许是输出的问题。