日期:2014-05-20 浏览次数:20804 次
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.PostMethod; public class HttpClientTest { private final static String REMOTE_FILE_URL = "http://202.120.96.77/dataViewPage/getFile.aspx?filename=GB-T 1.1-2000.pdf"; private final static int BUFFER = 1024; public static void main(String[] args) { HttpClient client = new HttpClient(); PostMethod httpGet = new PostMethod(REMOTE_FILE_URL); try { client.executeMethod(httpGet); InputStream in = httpGet.getResponseBodyAsStream(); FileOutputStream out = new FileOutputStream(new File("E:\\GB-T 1.1-2000.pdf")); byte[] b = new byte[BUFFER]; int len = 0; while ((len = in.read(b)) != -1) { out.write(b, 0, len); } in.close(); out.close(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { httpGet.releaseConnection(); } System.out.println("download, success!!"); } }
public static void main(String[] args) { HttpClient client = new HttpClient(); PostMethod httpGet = new PostMethod(REMOTE_FILE_URL); try { client.executeMethod(httpGet); InputStream in = httpGet.getResponseBodyAsStream(); FileOutputStream out = new FileOutputStream(new File("E:\\GB-T 1.1-2000.pdf")); byte[] b = new byte[BUFFER]; int len = 0; while ((len = in.read(b)) != -1) { out.write(b, 0, len); out.flush();//注意要清空流 } } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try{ in.close(); }catch(Exception e){ e.printStackTrace(); } try{ out.close(); }catch(Exception e){ e.printStackTrace(); } httpGet.releaseConnection(); } System.out.println("download, success!!"); }
------解决方案--------------------