想下载http://202.120.96.77/dataViewPage/getView.aspx?codeid=Y1里的pdf文件
总是不成功,哪位大哥帮改改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!!");
}
}
总是不成功,哪位大哥帮改改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!!");
}
}
解决方案 »
- 在公司里大家是如何缩进代码的?
- s2sh,删除jsp行数据。
- 请问国信朗讯这个公司如何,有知道的请进来看一看
- eclipse不支持ejb3.0吗?
- 在jboss中配置spring 对Log4j的增强!总是出错!
- 一个关于事件的问题
- 求助!!!EJB及JDBC及JDBC混合使用导致锁表
- java servlet例子怎么调试啊?
- 在java里怎么把一个字符转换成ASCII码保存在String中?
- 急急!!我在jb5+weblogic6里配置了EJB的东东,但BAS4.5不只怎样 set up?我也装了ias4.5呀
- 在jquery ajax中,如果从后台往前台传一个List集合,则在前台如何解析?
- svn
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!!");
}
得到<h1>bad request</h1>
但放到ie是可以下载到文件的...