一、背景用户上传了一张图片,图片到服务器后用户得到一个链接,可以将图片显示在浏览器上。二、实现假设项目名叫TestProject,文件放在项目根目录下的uploadImages文件夹下。①图片名为英文,可直接通过链接打开图片预览图片②图片名含有中文,通过Servlet将图片输出到浏览器上,使用图片在服务器上的绝对路径showImage.jsp预览图片showImageServletpublic void showImage(HttpServletRequest request, HttpServletResponse response) throws Exception{response.setContentType("text/html; charset=UTF-8");response.setContentType("p_w_picpath/jpeg");String fname = request.getParameter("filename");String newpath = new String(fname.getBytes("ISO-8859-1"), "UTF-8");String absolutePath = rootPath + newpath;FileInputStream fis = new FileInputStream(absolutePath);OutputStream os = response.getOutputStream();try{int count = 0;byte[] buffer = new byte[1024 * 1024];while ((count = fis.read(buffer)) != -1)os.write(buffer, 0, count);os.flush();}catch (IOException e){e.printStackTrace();}finally{if (os != null)os.close();if (fis != null)fis.close();}}
java web 图片显示_JavaWeb将图片显示在浏览器中
最新推荐文章于 2021-02-24 02:26:11 发布