直接用URL 然后再得到一个流就可以了public class GetURL { public static void main(String[] args) { InputStream in = null; OutputStream out = null; try { // Check the arguments if ((args.length != 1)&& (args.length != 2)) throw new IllegalArgumentException("Wrong number of args");
// Set up the streams URL url = new URL(args[0]); // Create the URL in = url.openStream(); // Open a stream to it if (args.length == 2) // Get an appropriate output stream out = new FileOutputStream(args[1]); else out = System.out;
// Now copy bytes from the URL to the output stream byte[] buffer = new byte[4096]; int bytes_read; while((bytes_read = in.read(buffer)) != -1) out.write(buffer, 0, bytes_read); } // On exceptions, print error message and usage message. catch (Exception e) { System.err.println(e); System.err.println("Usage: java GetURL <URL> [<filename>]"); } finally { // Always close the streams, no matter what. try { in.close(); out.close(); } catch (Exception e) {
http://hi.baidu.com/delete_h/blog/item/b6ae8359d8c5ce232934f013.html
把这个代码改改public void sendXML(String filePath,String _url) throws Exception{
String urlString = _url;
String xmlFile = filePath;
URL url = new URL(urlString); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();; File fileToSend=new File(xmlFile);
byte[] buf=new byte[(int)fileToSend.length()];
new FileInputStream(xmlFile).read(buf);
httpConn.setRequestProperty( "Content-Length",String.valueOf( buf.length ) );
httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction","××××××××");
httpConn.setRequestProperty("Accept","text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
httpConn.setRequestMethod( "POST" );
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write( buf );
out.close();
InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(),"utf-8");
BufferedReader in = new BufferedReader(isr);
String inputLine;
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("result.xml")));
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
bw.write(inputLine);
bw.newLine();
}
bw.close();
in.close();
}
public static void main(String[] args) {
InputStream in = null;
OutputStream out = null;
try {
// Check the arguments
if ((args.length != 1)&& (args.length != 2))
throw new IllegalArgumentException("Wrong number of args");
// Set up the streams
URL url = new URL(args[0]); // Create the URL
in = url.openStream(); // Open a stream to it
if (args.length == 2) // Get an appropriate output stream
out = new FileOutputStream(args[1]);
else out = System.out;
// Now copy bytes from the URL to the output stream
byte[] buffer = new byte[4096];
int bytes_read;
while((bytes_read = in.read(buffer)) != -1)
out.write(buffer, 0, bytes_read);
}
// On exceptions, print error message and usage message.
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java GetURL <URL> [<filename>]");
}
finally { // Always close the streams, no matter what.
try {
in.close();
out.close();
} catch (Exception e) {
}
}
}
}
String content = "";
try {
URL url = new URL(strUrl);
//得charset
String charset = SinoDetect.getInstance().getCharset(url);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.connect();
int code = uc.getResponseCode();
if(code != 200)
return content;
DataInputStream dis = new DataInputStream(uc.getInputStream());
InputStreamReader isr = new InputStreamReader(dis, charset);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
content += line.trim();
}
} catch (MalformedURLException e) {
System.out.println("getContent异常 1 : " + e.toString());
} catch (IOException e) {
System.out.println("getContent异常 2 : " + e.toString());
}
return content;
}