| 发表于:2008-01-23 11:45:15 楼主 |
小弟一直是做c++的,最近工作需求要求写一段java代码,实现http下载文件的功能,我在网上找了一段,以前用一直没有事,可是这向天突然出现问题了,大家去招啊. 代码如下: boolean getimpowerfile(string url, string param, string path) { //获取授权文件 url httpurl = null; httpurlconnection httpconn = null; try { httpurl = new url(url); httpconn = (httpurlconnection)httpurl.openconnection(); httpconn.setdooutput(true); httpconn.setdoinput(true); printwriter outs = new printwriter(httpconn.getoutputstream()); outs.print(param); outs.flush(); outs.close(); //这里发送请求,返回都没有问题,我用抓包工具看过. bufferedinputstream in = new bufferedinputstream(httpconn.getinputstream()); //设置授权文件路径 int nindex = path.indexof("tomcat"); path = path.substring(0, nindex); path += "server\\bin\\avnet\\profile\\kavnet.dat.new"; fileoutputstream fw=new fileoutputstream(path); int len = in.available(); //在这里缓存里的字节数返回永远都是0,以前这里就能正常返回大小. byte buf[] = new byte[len]; in.read(buf); fw.write(buf, 0 ,len); fw.flush(); fw.close(); in.close(); return true; }catch(exception e) { return false; } finally { if(httpconn!=null) httpconn.disconnect(); } } 以前这段代码都没有问题,这向天突然出现问题了,下载下来的文件变为空文件了,真是太奇怪了,不知道是程序写的有问题,还是其他原因,大伙有这方面的经历吗? |
|
|
|
|