java 读取服务器上的文件

Python011

java 读取服务器上的文件,第1张

http的话就用httpclient。open后,可以返回一个InputStream。这个就是你要读到文件流。

原理的话,参考你用浏览器打开这个链接显示的内容。

这个返回的是一个HTML网页,需要你解析出里面的文字(一般来说取body中间的内容就行)

其实对于这种文件一般用FTP来下载的。楼上写的那个不对,哈哈。

需要的话自己最好去查一下,怎么用,我有代码,不过告诉你的话也不太好?

URL url = new URL("http://你的地址")

URLConnection connection = url.openConnection()

InputStream is = connection.getInputStream()

BufferedReader br = new BufferedReader(new InputStreamReader(is,"gb2312"))

下面就是解析这个字符串来,自己来吧

java编程方法下载服务器上的文件到本地客服端,代码如下:

import java.io.BufferedWriter

import java.io.File

import java.io.FileOutputStream

import java.io.FileWriter

import java.io.IOException

import java.io.InputStream

import java.net.URL

import java.net.URLConnection

 

public class DownLoad {   

 public static void downloadFile(URL theURL, String filePath) throws IOException {  

   File dirFile = new File(filePath)

      if(!dirFile.exists()){ 

        //文件路径不存在时,自动创建目录

        dirFile.mkdir()

      }

  //从服务器上获取图片并保存

     URLConnection connection = theURL.openConnection()

     InputStream in = connection.getInputStream()  

     FileOutputStream os = new FileOutputStream(filePath+"\\123.png") 

     byte[] buffer = new byte[4 * 1024]  

     int read  

     while ((read = in.read(buffer)) > 0) {  

        os.write(buffer, 0, read)  

          }  

       os.close()  

       in.close()

  }   

     public static void main(String[] args) { 

      //下面添加服务器的IP地址和端口,以及要下载的文件路径

      String urlPath = "http://服务器IP地址:端口/image/123.png" 

      

      //下面代码是下载到本地的位置

      String filePath = "d:\\excel" 

  

      URL url = new URL(urlPath) 

  

          try { 

  

             downloadFile(url,filePath) 

  

           } catch (IOException e) { 

  

            e.printStackTrace() 

  

         } 

  

      }   

}