java获取远程文件(保证文件的完整性,不会出现无法打开的情况)
2013-01-30 11:36
573 查看
package com.shu.example; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.net.HttpURLConnection; import java.net.URL; public class FileUtil { /** * 获取远程文件 * @param remoteFilePath 远程文件路径 * @param localFilePath 本地文件路径 */ public void getFile(String remoteFilePath,String localFilePath){ URL urlfile = null; HttpURLConnection httpUrl = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; File f = new File(localFilePath); /* //如果需要设置代理时 String proxy = "192.168.224.12"; String port = "8080"; Properties systemProperties = System.getProperties(); systemProperties.setProperty("http.proxyHost",proxy); systemProperties.setProperty("http.proxyPort",port);*/ try{ urlfile = new URL(remoteFilePath); httpUrl = (HttpURLConnection)urlfile.openConnection(); httpUrl.connect(); bis = new BufferedInputStream(httpUrl.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream(f)); int len=2048; byte[] b = new byte[len]; while((len=bis.read(b))!=-1) { bos.write(b, 0, len); } bos.flush(); bis.close(); httpUrl.disconnect(); System.out.println("done~"); }catch(Exception e){ } } }
相关文章推荐
- chromium 在 Ubuntu 下出现无法打开 config 文件的错误,国外达人的方法。
- 为什么java读取配置文件,中文这样不会出现乱码
- Ubuntu下通过Firefox Opera Chromium浏览器直接执行java应用程序(打开java jnlp文件)实现在服务器远程虚拟控制台完成远程管理的方法
- java打开远程共享文件
- [导入]在 Visual Studio .NET IDE 内使用“从源代码管理打开”时出现无法读取项目文件的错误
- Cocos2d-x 3.1.1 学习日志17--移植出现error C1083: 无法打开包括文件:“extensions/ExtensionMacros.h”: No such file or,
- 头文件无法打开的情况之一
- 在sql2005中附加数据库时出现无法打开物理文件 "F:\ajax\test.mdf"。操作系统错误 5:"5(拒绝访问。)"解决办法
- JNI中用 javah.exe 产生头JNI-Style文件出现错误:无法访问 <class> 未找到 <class> 的类文件
- VS2010配置OpenCV2.4.7出现fatal error LNK1104:无法打开文件kernel32.lib的解决方法
- vs2010下,出现链接器工具错误 LNK1104“无法打开文件nafxcw.lib”,或者类似的错误
- 刚装ubuntu后,打开windows下的TXT文件就会发现无法显示中文,出现大量乱码
- windows 2008无法共享,无法打开 服务器管理器角色功能无法打开,office文件出现无
- VS2017出现LNK1104 无法打开文件gdi32.lib问题解决方法
- java在控制台编译时出现找不到或无法加载主类的情况
- 解决Java获取文件路径出现乱码的问题
- 打开qtcreator出现提示无法覆盖文件 /home/xxx/.config/Qtcreator/qtversion.xml : Permission denied
- 使用eclipse创建多文件java工程出现找不到或无法加载主类问题解决
- java中文件的编码(在建立text文本时,如果文件中只写联通或是联,再次打开的时候就会出现乱码,下面就是原理)
- 项目文件.NET下编译C++代码时出现fatal error C1083: 无法打开包括文件:“xuhk_JNIHelloWorld.h”: No such file or directory错误的解决方法。