servlet实现图片上传功能

网友投稿 552 2023-07-21

servlet实现图片上传功能

servlet实现图片上传功能

一个简单的servlet例子,实现图片的上传功能,上传的图片给  HttpServletResponse 对象

public class BackGroundLogoServlet extends HttpServlet

{

private static final Logger m_logger=Logger.getLogger (BackGroundLogoServlet. class);

@Override

public void init(ServletConfig config) throws ServletException {

super.ihttp://nit(config);

m_logger.debug ( "BackGroundLogoServlet init.");

}

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException{

response.setContentType( "image/png");

response.setHeader( "Access-Control-Allow-Origin", "*");

String fileName = request.getParameter( "filename");//获取参数值titlebar_logo.png

File file = new File( "D:\\"+ fileName);//读取D:\\titlebar_logo.png图片

FileInputStream fis = null;

BufferedOutputStream out= null;

try

{

fis = new FileInputStream(file);

out = new BufferedOutputStream(response.getOutputStream());

byte[] buffer= new byte[1024];

int len;

while((len=fis.read(buffer))!=-1)

{

//read the file from local disk

//write to client

out.write(buffer, 0, len);

out.flush();

m_logger.debug ( "background pic upload success !");

}

}

catch (FileNotFoundException e)

{

http:// try

{

response.reset();

//set content type once again

response.setContentType("text/html;charset=utf-8" );

//give error message to client

response.getWriter().println( "文件未找到" );

}

catch (IOException e1)

{

e1.printStackTrace();

}

e.printStackTrace();

}

catch (IOException e)

{

e.printStackTrace();

}

finally

{

try

{

if(fis!= null){

fis.BlwlOcclose();

}

if(out!= null){

out.close();

}

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ClassLoader类加载源码解析
下一篇:深入理解JVM之Class类文件结构详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~