下载:
//下载文件
public void downloadFile(HttpServletResponse response, String url)throws IOException { response.reset(); response.setContentType("application/x-msdownload; charset=GBK"); String fileName = new String(url.getBytes("gb2312"), "ISO-8859-1"); response.setHeader("Content-Disposition", "attachment; filename="" + fileName); // 获取文件路径 String filePath="d:\upload\"+url; File file=new File(filePath); FileInputStream in = new FileInputStream(file); BufferedInputStream bis=new BufferedInputStream(in); byte[] buffer = new byte[512]; int i=bis.read(buffer); OutputStream outputStream=response.getOutputStream(); while(i!=-1){ outputStream.write(buffer,0,i); i=bis.read(buffer); } outputStream.flush(); in.close(); bis.close(); outputStream.close(); }
展示图片
public static final String showImg(HttpServletResponse response, String path) throws IOException { FileInputStream fileIs=null; try { fileIs = new FileInputStream(path); } catch (Exception e) { return null; } int i=fileIs.available(); //得到文件大小 byte data[]=new byte[i]; fileIs.read(data); //读数据 response.setContentType("image/*"); //设置返回的文件类型 图片 OutputStream outStream=response.getOutputStream(); //得到向客户端输出二进制数据的对象 outStream.write(data); //输出数据 outStream.flush(); outStream.close(); fileIs.close(); return path; }
-