File uploading and downloading (c) download file

 download:

//下载文件
    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(); }

 

Show pictures

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 (); // get the file size    
          byte Data [] = new new  byte [I];   
          fileIs.read (Data);   // read data    
          the response.setContentType ( "Image / *"); // set the type of picture file return 
          the OutputStream the outStream response.getOutputStream = (); // get the object output binary data to the client    
          outStream.write (data);   // output data       
          outStream.flush ();  
          outStream.close();   
          fileIs.close();
          return path;
    }

 

-

Guess you like

Origin www.cnblogs.com/DarGi2019/p/12132348.html