RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
servlet中怎么实现图片上传功能

本篇文章给大家分享的是有关servlet中怎么实现图片上传功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

10年积累的做网站、成都网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有大同免费网站建设让你可以放心的选择与我们合作。

一个简单的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://scyingshan.cn/article/gcsgee.html