有关java远程读取文件的方法
1
package word;
2
3
import java.io.FileOutputStream;
4
import java.io.InputStream;
5
import java.net.HttpURLConnection;
6
7
public class FileRead {
8
9
public static void main(String[] args) {
10
java.net.URL urlfile = null;
11
HttpURLConnection httpUrl = null;
12
13
try {
14
15
// 连接指定的网络资源,获取网络输入流
16
urlfile = new java.net.URL(
17
"http://10.142.202.12/ftp/Y0131000000168/D0011000006537.jpg");
18
httpUrl = (HttpURLConnection) urlfile.openConnection();
19
httpUrl.connect();
20
int bytesum = 0;
21
int byteread = 0;
22
InputStream inStream = httpUrl.getInputStream();
23
FileOutputStream fs = new FileOutputStream("c:/1.jpg");
24
byte[] buffer = new byte[1444];
25
while ((byteread = inStream.read(buffer)) != -1) {
26
bytesum += byteread;
27
fs.write(buffer, 0, byteread);
28
}
29
30
} catch (Exception e) {
31
32
}
33
}
34
}
35
package word;2

3
import java.io.FileOutputStream;4
import java.io.InputStream;5
import java.net.HttpURLConnection;6

7
public class FileRead {8

9
public static void main(String[] args) {10
java.net.URL urlfile = null; 11
HttpURLConnection httpUrl = null; 12

13
try {14

15
// 连接指定的网络资源,获取网络输入流16
urlfile = new java.net.URL(17
"http://10.142.202.12/ftp/Y0131000000168/D0011000006537.jpg");18
httpUrl = (HttpURLConnection) urlfile.openConnection();19
httpUrl.connect();20
int bytesum = 0;21
int byteread = 0;22
InputStream inStream = httpUrl.getInputStream();23
FileOutputStream fs = new FileOutputStream("c:/1.jpg");24
byte[] buffer = new byte[1444];25
while ((byteread = inStream.read(buffer)) != -1) {26
bytesum += byteread;27
fs.write(buffer, 0, byteread);28
} 29
30
} catch (Exception e) {31

32
} 33
}34
}35

相关文档(Relevant Entries)
Posted on April 11, 2007 12:41 AM
| Permalink | | | TrackBacks (0)
TrackBack URL for this entry:
http://www.wujianrong.com/mt-tb.cgi/4983
http://www.wujianrong.com/mt-tb.cgi/4983


