网站建设资讯

NEWS

网站建设资讯

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.init(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)      {        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.close();        }        if(out!= null){          out.close();        }      }      catch (IOException e)      {        e.printStackTrace();      }    }         }

以上就是servlet中怎么实现图片上传功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


文章标题:servlet中怎么实现图片上传功能-创新互联
分享地址:http://cdweb.net/article/jecjo.html