📄 urlfiletransfer.java
字号:
package edu.ou.kmi.buddyspace.utils;
/*
* URLFileTransfer.java
*
* Project: BuddySpace
* (C) Copyright Knowledge Media Institute 2002
*
*
* Created on 21 August 2002, 15:45
*/
import java.net.*;
import java.io.*;
import java.util.*;
/**
* <code>URLFileTransfer</code> provides web up/downloading functionality.
*
* @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
*/
public class URLFileTransfer {
static private final int BUFFER_LEN = 512;
/** Downloads and saves file from url using given proxy */
public static void downloadURL(URL url, File file, String proxy, String proxyPort)
throws IOException {
byte buffer[] = new byte[BUFFER_LEN];
FileOutputStream output;
BufferedInputStream input;
try {
output = new FileOutputStream(file);
Properties prop = System.getProperties();
if (proxy != null && proxyPort != null) {
prop.put("http.proxyHost", proxy);
prop.put("http.proxyPort", proxyPort);
}
else {
prop.put("http.proxyHost", "");
prop.put("http.proxyPort", "");
}
HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
InputStream i = urlCon.getInputStream();
input = new BufferedInputStream(i);
} catch (FileNotFoundException e) {
System.out.println("cannot open output file");
throw new IOException("Cannot open output file");
//return;
} catch (SecurityException e) {
//System.out.println("cannot write into output file");
throw new IOException("Cannot write into output file");
//return;
} catch (IOException e) {
System.out.println("cannot open URL: " + e.getMessage());
throw new IOException("Cannot open URL: " + e.getMessage());
//return;
}
int num;
int off = 0;
try {
while ((num = input.read(buffer, off, BUFFER_LEN)) != -1) {
output.write(buffer, off, num);
}
input.close();
output.close();
} catch (IOException e) {
System.out.println("IO error");
throw new IOException("IO error");
//return;
}
}
/** Uploads given file onto url using given proxy */
public static void uploadURL(File file, URL url, String proxy, String proxyPort)
throws IOException {
byte buffer[] = new byte[BUFFER_LEN];
FileInputStream input;
BufferedOutputStream output;
HttpURLConnection urlCon;
try {
input = new FileInputStream(file);
Properties prop = System.getProperties();
if (proxy != null && proxyPort != null) {
prop.put("http.proxyHost", proxy);
prop.put("http.proxyPort", proxyPort);
}
else {
prop.put("http.proxyHost", "");
prop.put("http.proxyPort", "");
}
urlCon = (HttpURLConnection) url.openConnection();
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.setRequestMethod("PUT");
OutputStream o = urlCon.getOutputStream();
output = new BufferedOutputStream(o);
} catch (FileNotFoundException e) {
System.out.println("cannot open output file");
throw new IOException("Cannot open output file");
//return;
} catch (SecurityException e) {
System.out.println("cannot write into output file");
throw new IOException("Cannot write into output file");
//return;
} catch (IOException e) {
System.out.println("cannot open URL: " + e.getMessage());
throw new IOException("Cannot open URL: " + e.getMessage());
//return;
}
int num;
int off = 0;
try {
while ((num = input.read(buffer, off, BUFFER_LEN)) != -1) {
output.write(buffer, off, num);
}
input.close();
output.close();
// reads the response
BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
;//System.out.println(inputLine);
in.close();
} catch (IOException e) {
System.out.println("IO error");
throw new IOException("IO error");
//return;
}
}
// *** testing ***
/*protected static void uploadTut() {
try {
String stringToReverse = "hnupe!";//URLEncoder.encode("nazdar");
URL url = new URL("http://whatever.open.ac.uk/upload/vtipy.txt");
URLConnection connection = url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
((HttpURLConnection)connection).setRequestMethod("PUT");
BufferedOutputStream out = new BufferedOutputStream(
connection.getOutputStream());
byte dr[] = stringToReverse.getBytes();
out.write(dr, 0, dr.length);
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} catch (Exception e) {
System.out.println("failed");
}
}*/
/*protected static void testDownload() {
File f = new File("J:/t.txt");
URL u;
try {
u = new URL("http://komzak.webz.cz/index.html");
URLFileTransfer.downloadURL(u, f, "wwwcache.open.ac.uk", "80");
} catch (MalformedURLException e) {
System.out.println("invalid URL");
return;
} catch (IOException e) {
System.out.println("download failed");
return;
}
}*/
/*protected static void testUpload() {
File f = new File("J:/t.jpg");
URL u;
try {
u = new URL("http://whatever.open.ac.uk/upload/t.jpg");
URLFileTransfer.uploadURL(f, u, null, null);
} catch (MalformedURLException e) {
System.out.println("invalid URL");
return;
} catch (IOException e) {
System.out.println("upload failed");
return;
}
}*/
/*static public void main(String args[]) {
//testDownload();
testUpload();
//uploadTut();
}*/
// *** directly to socket ***
/*protected static void downloadURL2(File file) {
byte buffer[] = new byte[BUFFER_LEN];
FileOutputStream output;
BufferedInputStream input;
try {
output = new FileOutputStream(file);
Socket s = new Socket("wwwcache.open.ac.uk", 80);
OutputStream o = s.getOutputStream();
o.write(new String("GET http://komzak.webz.cz/index.html HTTP/1.0 \nHost: komzak.webz.cz\n\n").getBytes());
InputStream i = s.getInputStream();
input = new BufferedInputStream(i);
} catch (FileNotFoundException e) {
System.out.println("cannot open output file");
return;
} catch (SecurityException e) {
System.out.println("cannot write into output file");
return;
} catch (IOException e) {
System.out.println("cannot open URL: " + e.getMessage());
return;
}
int num;
int off = 0;
try {
while ((num = input.read(buffer, off, BUFFER_LEN)) != -1) {
output.write(buffer, off, num);
}
input.close();
output.close();
} catch (IOException e) {
System.out.println("IO error");
return;
}
}
protected static void testDownload2() {
File f = new File("J:/t.txt");
URLFileTransfer.downloadURL2(f);
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -