日期:2014-05-18  浏览次数:20682 次

想要访问一个地址,然后把下载的东西放到一个文件夹里,求思路,有需要用的类最好!
例如:这么一个地址http://data.daily.taobao.net/data/../../../total.zip
 你访问这个地址直接会下载这个文件,我想用Java代码访问这个地址,然后把下载的zip文件放入一个文件夹里,求需要用什么知识,有实例代码么?

------解决方案--------------------
HttpURLConnection urlConn = null;
                
                        url = new URL(strUrl);
                        urlConn = (HttpURLConnection) url.openConnection();
                        urlConn.setConnectTimeout(1000 * 20);
                       InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
获取到inputstream,你就写到你的文件夹下去
                        
------解决方案--------------------
String filePath=request.getRealPath("/..");
   String filename = "total.zip";
       File fileLoad = new File(filePath, filename);  

       FileInputStream in = null; // 输入流  
       OutputStream out = response.getOutputStream();  
       byte b[] = new byte[1024];  
  
        try {  
 
             response.setContentType("application/x-msdownload;");  
  
            response.setHeader("Content-disposition", "attachment; filename="  
                   + new String(filename.getBytes("UTF-8"), "UTF-8"));  
  
            // download the file.  
            in = new FileInputStream(fileLoad);  
           int n = 0;  
            while ((n = in.read(b)) != -1) {  
                out.write(b, 0, n);  
           }&nbs