佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > JSP實(shí)現(xiàn)遠(yuǎn)程文件下載保存到服務(wù)器指定目錄中的方法

JSP實(shí)現(xiàn)遠(yuǎn)程文件下載保存到服務(wù)器指定目錄中的方法

熱門(mén)標(biāo)簽:科大訊飛語(yǔ)音識(shí)別系統(tǒng) Mysql連接數(shù)設(shè)置 電子圍欄 阿里云 服務(wù)器配置 Linux服務(wù)器 團(tuán)購(gòu)網(wǎng)站 銀行業(yè)務(wù)

本文實(shí)例講述了JSP實(shí)現(xiàn)遠(yuǎn)程文件下載保存到服務(wù)器指定目錄中的方法。分享給大家供大家參考,具體如下:

%@page import="java.net.*,java.io.*"%>
%!
 public boolean saveUrlAs(String photoUrl, String fileName) {
//此方法只能用戶(hù)HTTP協(xié)議
  try {
   URL url = new URL(photoUrl);
   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
   DataInputStream in = new DataInputStream(connection.getInputStream());
   DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));
   byte[] buffer = new byte[4096];
   int count = 0;
   while ((count = in.read(buffer)) > 0) {
    out.write(buffer, 0, count);
   }
   out.close();
   in.close();
   return true;
  }
  catch (Exception e) {
   return false;
  }
 }
public String getDocumentAt(String urlString) {
//此方法兼容HTTP和FTP協(xié)議
  StringBuffer document = new StringBuffer();
  try {
   URL url = new URL(urlString);
   URLConnection conn = url.openConnection();
   BufferedReader reader = new BufferedReader(new InputStreamReader(conn.
     getInputStream()));
   String line = null;
   while ( (line = reader.readLine()) != null) {
    document.append(line + "\n");
   }
   reader.close();
  }
  catch (MalformedURLException e) {
   System.out.println("Unable to connect to URL: " + urlString);
  }
  catch (IOException e) {
   System.out.println("IOException when connecting to URL: " + urlString);
  }
  return document.toString();
 }
%>
%
//測(cè)試
 String photoUrl = "http://ad4.sina.com.cn/200601/12/43932_750450.jpg";
 String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
 String filePath = "C:/test/";
 boolean flag = saveUrlAs(photoUrl, filePath + fileName);
 out.println("Run ok!\nBR>Get URL file " + flag);
%>

希望本文所述對(duì)大家JSP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • JSP隱含對(duì)象response實(shí)現(xiàn)文件下載
  • jsp實(shí)現(xiàn)Servlet文件下載的方法
  • JSP文件下載功能的4種方法
  • JSP response對(duì)象實(shí)現(xiàn)文件下載的兩種方式
  • JSP隱含對(duì)象response實(shí)現(xiàn)文件下載的兩種方法
  • jsp 文件下載示例代碼
  • jsp文件下載功能實(shí)現(xiàn)代碼

標(biāo)簽:衡水 衢州 江蘇 大理 棗莊 廣元 蚌埠 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP實(shí)現(xiàn)遠(yuǎn)程文件下載保存到服務(wù)器指定目錄中的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266
    龙山县| 石嘴山市| 汝南县| 教育| 临沧市| 蒙山县| 卢氏县| 鸡泽县| 万州区| 盘山县| 顺义区| 巴东县| 芦山县| 洪洞县| 永德县| 赤城县| 屏南县| 华坪县| 清苑县| 菏泽市| 涞源县| 精河县| 剑川县| 丰宁| 元朗区| 辛集市| 伊宁市| 新闻| 永川市| 彭泽县| 齐齐哈尔市| 十堰市| 黑山县| 高台县| 武鸣县| 嫩江县| 苍梧县| 马公市| 绍兴县| 清流县| 新乡县|