您的位置:程序门 -> java -> web 开发



第一次在java版块发帖,请大家救命啊!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


第一次在java版块发帖,请大家救命啊!
发表于: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();          
    }
}
以前这段代码都没有问题,这向天突然出现问题了,下载下来的文件变为空文件了,真是太奇怪了,不知道是程序写的有问题,还是其他原因,大伙有这方面的经历吗?
发表于:2008-01-23 12:50:551楼 得分:0
在available行之前加一句

java code
int firstchar = in.read();


试试
发表于:2008-01-23 13:09:292楼 得分:0
我也有过,最后用jspsmartupload控件写的下载页面。这个问题也没解决,关注一下!
发表于:2008-01-23 13:35:353楼 得分:0
to   dracularking   :
  我试了下,还是不行;这上代码是上半年写的,一直都没有问题;最近突然下载文件下来是空的,经过仔细分析问题就出在这里,如果代码没有问题,那会不会有其他什么原因呢?
发表于:2008-01-23 13:39:014楼 得分:0
图书出售,
java2核心技术卷i:基础知识(原书第7版)
详细说明:http://product.dangdang.com/product.aspx?product_id=9167161   原价:88

java2核心技术,卷ii:高级特性(原书第7版)
详细说明:http://product.dangdang.com/product.aspx?product_id=9155128   原价:108

java   web开发详解:xml+xslt+servlet+jsp深入剖析与实例应用(附光盘)  
详细说明:http://product.dangdang.com/product.aspx?product_id=9167911   原价:99

effective   java中文版
详细说明:http://product.dangdang.com/product.aspx?product_id=707050   原价:39

java与xslt  
详细说明:http://product.dangdang.com/product.aspx?product_id=725436   原价:55

java网络编程(第3版)——o'reilly   java系列  
详细说明:http://product.dangdang.com/product.aspx?product_id=9062293   原价:85

junit   in   action   中文版——java人的工具箱系列  
详细说明:http://product.dangdang.com/product.aspx?product_id=8935882   原价:39

spring专业开发指南  
详细说明:http://product.dangdang.com/product.aspx?product_id=9202403   原价:80

spring   2.0技术手册(附光盘)
详细说明:http://product.dangdang.com/product.aspx?product_id=9303590   原价:49.8

深入浅出hibernate  
详细说明:http://product.dangdang.com/product.aspx?product_id=8991354   原价:59

struts   2   权威指南:基于webwork核心的mvc开发(附光盘)  
详细说明:http://product.dangdang.com/product.aspx?product_id=20029121&mode=new   原价:79

oracle高级编程  
详细说明:http://product.dangdang.com/product.aspx?product_id=9296670   原价:69

sql   server   2005宝典(附盘)  
详细说明:http://product.dangdang.com/product.aspx?product_id=9247242   原价:79

以上书籍完好无损,均以4.5折出售,如需邮寄,邮费自付。qq:492717188   tel:13450830512   谢谢!
发表于:2008-01-23 13:40:565楼 得分:0
有可能是服务器端发生了变化。
如果服务器发来的字节流没有作任何标记,只是在发完一次数据后flush(),则在客户端可以用inputstream.available()来判断流的长度。
发表于:2008-01-23 15:53:296楼 得分:0
经过仔细查找资料,在该行代码处:
int       len       =       in.available();              
调用available()函数有安全隐患,jdk   api中描述:
中文:returns   the   number   of   bytes   that   can   be   read   from   this   input   stream   without   blocking.
英文:返回在不发生阻塞的情况下,可读取的字节数;
隐患:问题就处在不发生阻塞的情况下,在网络环境中可能存在阻塞,很多时候会导致返回失效,结果为0;所以在网络环境切不能够使用这种方式获取缓存区大小.

解决办法:
1.获取缓存区大小采取获取返回的http的content-length的大小;
2.将上术代码改为:
byte   buf[]   =   new   byte[10240];
int   nsize   =   0;
.....
nsize   =   in.read(buf);
fw.write(buf,   0   ,nsize);

.....
上面只能免费满足我的要求,因为我下载的文件不超过1k;
最完整的方案是:
byte   buf[]   =   new   byte[1024];
int   nsize   =   0;
.....
while   ((nsize   =   in.read(buf))   !=   -1)
{
      fw.write(buf,   0   ,nsize);
}
....
 
不过不知道是什么原因,只要第二次调用in.read(buf)就会出现异常,由于时间问题以及本人对java不是很了解,就不深入研究了,先暂且满足我下载不超过1k文件的要求.

希望java版块的大牛人能够告诉小弟一下,为什么两次调用in.read(buf)会出抛出异常的原因,在此谢过,祝大家新年快乐!


发表于:2008-01-23 17:24:217楼 得分:0
挖哈哈!!!     你代码   都写错了~     还玩个六   ,   楼上的代码段    
byte       buf[]       =       new       byte[1024];  
int       nsize       =       0;  
.....  
while       ((nsize       =       in.read(buf))       !=       -1)  
{  
            fw.write(buf,       0       ,nsize);  
}  


中的  
while       ((nsize       =       in.read(buf))       !=       -1)  
{  
            fw.write(buf,       0       ,nsize);  
}  

应写为    
while       ((nsize       =       in.read(buf))     > 0)  
{  
            fw.write(buf,       0       ,nsize);  
}  

这么写才没问题,这块   代码段的意思是   增加   流的缓冲区   大小   ,可以写   文件速度加快   ,这种例子网上很多的
发表于:2008-01-23 17:31:078楼 得分:0
其实本身   你在   研究下载的问题   上   已经   进   了一个误区   ,   首先你要了解   java   实现文件下载的原理是什么   :    

1   在   servlet   中   通过   response   对象     获取     outputstream
2   使用流   将下载的文件   用   inputstream   读进来    
3   在   outputstream     中   通过   inputstream     读的   流   ,使用   outputstream   来写   需要下载的文件.
4   关闭那两个流   就完了     .    

用不着   什么   smartupload   还要引包,直接   写servlet     不就完了.  
发表于:2008-01-23 17:49:299楼 得分:0
这是用struts中写下载的,随便写的不知道对你有没用
smartuploadform   smartuploadform=(smartuploadform)form;
                formfile   file1   =   smartuploadform.getupload();
               
                system.out.println("file===="+file1);
                inputstream   is   =   file1.getinputstream();   //读入文件

                response.reset();          
                response.setcontenttype("bin");      
                response.addheader("content-disposition","attachment;filename=test.xls");  
                byte[]       buffer;      
                int       length=(new       long(file1.getfilesize()).intvalue());      
                buffer=new       byte[length];      
                system.out.println("length==="+length);
                bufferedinputstream   bis   =   new   bufferedinputstream(is);
                fileoutputstream   out1=new   fileoutputstream("c://test.xls");
                while   (bis.read(buffer)   >   0)   {
                        out1.write(buffer);
                }
                out1.flush();
                servletoutputstream   out   =   response.getoutputstream();  
                while   (bis.read(buffer)   >   0)   {
                        out.write(buffer);
                }
                out.flush();
                bis.close();
                is.close();
                return   null;
发表于:2008-01-23 17:56:2510楼 得分:0
给你一段我写的文件下载   :    
public   class   reportexportfileservlet   extends   httpservlet   {
        public   reportexportfileservlet()   {
super();
}
                public   void   dopost(httpservletrequest   request,   httpservletresponse   response)
throws   servletexception,   ioexception   {
               

                //要下载的文件名,   这个文件名   为文件   头的名字  
                string   filename   =   timeutil.getcurrentlytimesecond("",   "",   "")   +".zip"; //     这步   是必不可少的   ,因为这步解决了   文件头的乱码问题   !!!
string   fileexactname   =   urlencoder.encode(filename,   "utf-8");
//设置文件头
response.setheader("content-disposition","attachment;   filename="   +   fileexactname);
//设置文本类型为下载                        
                response.setcontenttype("application/x-msdownload");
                downloadfile(response.getoutputstream()filename   );
                }

                private   void   downloadfile(outputstream   out,string   inputzipfile)   {
       
        fileinputstream   in   =   null;
       
        try   {
in   =   new   fileinputstream(inputzipfile);   //   读入文件
out.flush();
int   aread   =   0;
while   ((aread   =   in.read())   !=   -1   &   in   !=   null)   {
out.write(aread);
}
out.flush();
}   catch   (throwable   e)   {
//   log.error("filedownload   doget()   io   error!",e);
}   finally   {
try   {
in.close();
out.close();
}   catch   (throwable   e)   {
//   log.error("filedownload   doget()   io   close   error!",e);
}
}
        }

}
发表于:2008-01-23 17:58:2611楼 得分:0
我写了这么多,你在不会我就该骂人了!     当时我可是自己在   网上找   东西,用自己的   想法写出来的   ,这个   东西能下载   几百兆的东西呢   !     在   移动   服务器   跑n   年了


快速检索

最新资讯
热门点击