欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

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

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

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

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) { // 在線打開方式
   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中批量下載文件,將下載多個文件打包成zip文件下載。


 //批量文件下載(將多個文件打包成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)); 
 // 需要同時下載的多個文件
 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)的字符編碼,不然會變成亂碼 
 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("文件下載錯誤");
 } 
 }

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

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

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

廣告

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

成都網(wǎng)站建設(shè)公司
综合激情网激情五月天| 亚洲国产色一区二区三区| 国产精品自拍小视频91| 欧美激情网页一区三区| 国产av手机自拍看片| 亚洲一二三无人区是什么| 91精品国产高清一区二区性色| 色综合久久天天射天天干| 日韩成人中文字幕电影| 欧美黄片在线播放视频| 6080yy精品一区二区三区| 国产一级内射麻豆91| 色欧美在线视频观看日韩| 久草区免费在线视频播放| 免费看国产一级黄色大片| 少妇的诱惑免费在线看| 人妻有码一区二区三区| 日韩在线一区二区视频观看| 久热视频这里只有精品99| 九七青青草视频在线观看| 99精品午夜福利在线| 亚洲精品人妻av在线| 亚洲成人大片免费在线观看| 超碰91人人草人人干| 婷婷av一区二区三区| 国产亚洲中文久久网久久| 亚洲精品成人在线国产| 亚洲无线码一区国产欧美国日| 国产农村熟妇av国语对白| 91日本视频在线播放| 日本特黄高清免费大片| 清纯唯美亚洲自拍第一页| 亚洲高清成人在线观看| 日韩欧美一区二区黄色| 日韩久久精品五月综合| 国产日韩欧美高清免费视频| 亚洲国产黄片在线播放| 色综合久久婷婷色综合网| 国产日韩亚洲欧美在线| 亚洲国产精品性色av| 午夜福利视频在线一区|