java
package com.yunfengtech.solution; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import com.yunfengtech.solution.util.PropertyUtils; @WebServlet("/ivs") public class ImgAndVideoServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static Logger logger = Logger.getLogger("default"); private static final String JPG = "image/jpeg;charset=GB2312"; private static final String PNG = "image/png;charset=GB2312"; private static final String MP4 = "video/mpeg4;charset=utf-8"; /** * @see HttpServlet#HttpServlet() */ public ImgAndVideoServlet() { super(); } ServletContext sc; public void init(ServletConfig config) throws ServletException { super.init(config); sc = config.getServletContext(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request,HttpServletResponse response) { OutputStream output = null; InputStream imageIn = null; FileInputStream fis = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { output = response.getOutputStream();// 得到输出流 String ft = request.getParameter("ft"); String fileDir = ""; String fileName = request.getParameter("fn") ;//URLDecoder.decode(request.getParameter("fn"),"UTF-8"); fileName = new String(fileName.getBytes("iso-8859-1"),"utf-8"); response.reset(); if(ft != null && ft.equals("mp4")){ fileDir = PropertyUtils.getProperty("videoPath"); response.setContentType(MP4);// 设定输出的类型 response.setHeader("Content-Disposition", "attachment;filename=" + fileName); File file = new File(fileDir + fileName); int len = (int) file.length(); byte[] buf = new byte[4096]; fis = new FileInputStream(file); len = fis.read(buf); while (len != -1) { output.write(buf, 0, len); len = fis.read(buf); } output.flush(); }else if(fileName.toLowerCase().endsWith(".jpg") || fileName.toLowerCase().endsWith(".png")){ if(fileName.toLowerCase().endsWith(".jpg") ){ response.setContentType(JPG);// 设定输出的类型 }else{ response.setContentType(PNG);// 设定输出的类型 } fileDir = PropertyUtils.getProperty("imagePath"); imageIn = new FileInputStream(fileDir + fileName);//文件流 bis = new BufferedInputStream(imageIn);//输入缓冲流 bos = new BufferedOutputStream(output);//输出缓冲流 byte data[] = new byte[4096];//缓冲字节数 int size = 0; size = bis.read(data); while (size != -1){ bos.write(data,0,size); size = bis.read(data); } bos.flush();//清空输出缓冲流 } } catch (IOException e) { logger.error("获取文件流出错:"+e); e.printStackTrace(); } finally{ if(output != null){ try { output.close(); } catch (IOException e) { e.printStackTrace(); } } if(imageIn != null){ try { imageIn.close();// 关闭文件流 } catch (IOException e) { e.printStackTrace(); } } if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if(bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if(bos != null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
servlet
<servlet> <servlet-name>ImgAndVideoServlet</servlet-name> <servlet-class>com.yunfengtech.solution.ImgAndVideoServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ImgAndVideoServlet</servlet-name> <url-pattern>/ivs</url-pattern> </servlet-mapping>
js
setImage : function(obj, val){ var imageUrl = "/np/ivs?fn=" + val; $(obj).parent().find("div[data-type='image']").html("<img src="+imageUrl+" style= 'width:40px; hight:40px;'/>"); }