内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

Java中下載文件的方法有哪些-創(chuàng)新互聯(lián)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Java中下載文件的方法有哪些,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

為黃驊等地區(qū)用戶(hù)提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及黃驊網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、黃驊網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶(hù)提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶(hù)的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

1.以流的方式下載.

public HttpServletResponse download(String path, HttpServletResponse response) {
 try {
  // path是指欲下載的文件的路徑。
  File file = new File(path);
  // 取得文件名。
  String filename = file.getName();
  // 取得文件的后綴名。
  String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
 
  // 以流的形式下載文件。
  InputStream fis = new BufferedInputStream(new FileInputStream(path));
  byte[] buffer = new byte[fis.available()];
  fis.read(buffer);
  fis.close();
  // 清空response
  response.reset();
  // 設(shè)置response的Header
  response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
  response.addHeader("Content-Length", "" + file.length());
  OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
  response.setContentType("application/octet-stream");
  toClient.write(buffer);
  toClient.flush();
  toClient.close();
 } catch (IOException ex) {
  ex.printStackTrace();
 }
 return response;
 }

2.下載本地文件

public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
 // 下載本地文件
 String fileName = "Operator.doc".toString(); // 文件的默認(rèn)保存名
 // 讀到流中
 InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路徑
 // 設(shè)置輸出的格式
 response.reset();
 response.setContentType("bin");
 response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
 // 循環(huán)取出流中的數(shù)據(jù)
 byte[] b = new byte[100];
 int len;
 try {
  while ((len = inStream.read(b)) > 0)
  response.getOutputStream().write(b, 0, len);
  inStream.close();
 } catch (IOException e) {
  e.printStackTrace();
 }
 }

3.下載網(wǎng)絡(luò)文件

public void downloadNet(HttpServletResponse response) throws MalformedURLException {
  // 下載網(wǎng)絡(luò)文件
  int bytesum = 0;
  int byteread = 0;
 
  URL url = new URL("windine.blogdriver.com/logo.gif");
 
  try {
   URLConnection conn = url.openConnection();
   InputStream inStream = conn.getInputStream();
   FileOutputStream fs = new FileOutputStream("c:/abc.gif");
 
   byte[] buffer = new byte[1204];
   int length;
   while ((byteread = inStream.read(buffer)) != -1) {
    bytesum += byteread;
    System.out.println(bytesum);
    fs.write(buffer, 0, byteread);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

4.支持在線(xiàn)打開(kāi)的方式

public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
  File f = new File(filePath);
  if (!f.exists()) {
   response.sendError(404, "File not found!");
   return;
  }
  BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
  byte[] buf = new byte[1024];
  int len = 0;
 
  response.reset(); // 非常重要
  if (isOnLine) { // 在線(xiàn)打開(kāi)方式
   URL u = new URL("file:///" + filePath);
   response.setContentType(u.openConnection().getContentType());
   response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
   // 文件名應(yīng)該編碼成UTF-8
  } else { // 純下載方式
   response.setContentType("application/x-msdownload");
   response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
  }
  OutputStream out = response.getOutputStream();
  while ((len = br.read(buf)) > 0)
   out.write(buf, 0, len);
  br.close();
  out.close();
 }

JAVA中批量下載文件,將下載多個(gè)文件打包成zip文件下載。


 //批量文件下載(將多個(gè)文件打包成zip包下載)
 public static void batchDownLoadFile(HttpServletRequest request,HttpServletResponse response,String filename,String[] filepath,String[] documentname,String loginname){ 
 byte[] buffer = new byte[1024];
 Date date=new Date();
 //生成zip文件存放位置
 String strZipPath = Constant.exportAddress +loginname+date.getTime()+".zip";
 File file=new File(Constant.exportAddress);
 if(!file.isDirectory() && !file.exists()){
 //創(chuàng)建單層目錄
 // f.mkdir();
 // 創(chuàng)建多層目錄
 file.mkdirs();
 }
 try { 
 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath)); 
 // 需要同時(shí)下載的多個(gè)文件
 for (int i = 0; i < filepath.length; i++) {
 File f=new File(filepath[i]);
 FileInputStream fis = new FileInputStream(f);
 System.out.println(documentname[i]);
 out.putNextEntry(new ZipEntry(documentname[i])); 
 //設(shè)置壓縮文件內(nèi)的字符編碼,不然會(huì)變成亂碼 
 out.setEncoding("GBK"); 
 int len; 
 // 讀入需要下載的文件的內(nèi)容,打包到zip文件 
 while ((len = fis.read(buffer)) > 0) { 
 out.write(buffer, 0, len); 
 } 
 out.closeEntry(); 
 fis.close(); 
 } 
 out.close(); 
 PublicMethod.downLoadFile(request, response, strZipPath, filename+".zip");
 File temp=new File(strZipPath);
 if(temp.exists()){
 temp.delete();
 }
 } catch (Exception e) { 
 System.out.println("文件下載錯(cuò)誤");
 } 
 }

上述就是小編為大家分享的Java中下載文件的方法有哪些了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞標(biāo)題:Java中下載文件的方法有哪些-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://www.rwnh.cn/article42/cojhec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)網(wǎng)站排名、品牌網(wǎng)站制作、企業(yè)網(wǎng)站制作、App開(kāi)發(fā)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司
射阳县| 灵寿县| 饶河县| 麻栗坡县| 前郭尔| 黔江区| 上饶市| 阿拉尔市| 瑞昌市| 吉隆县| 土默特右旗| 邻水| 台东市| 吉林省| 昭平县| 五家渠市| 高要市| 班玛县| 永丰县| 抚州市| 盐山县| 自贡市| 闽清县| 德州市| 漾濞| 修武县| 洛扎县| 英山县| 白山市| 石景山区| 温宿县| 二手房| 海南省| 海兴县| 甘德县| 新化县| 卓资县| 邵阳市| 明光市| 余姚市| 武鸣县|