HttpURLConnection相关

旅人葉 2017-01-20 11:12:09
前任的代码,没有注释解说,没有文档,以前没用过这种方式,只能看懂是通过文件流读取,然后写入到本地文件中,但是那个fileUrl里面应该填什么只能慢慢测试,然而项目着急,哪位大神有时间指导一下,


public static void downloadFile(String fileUrl, String localFile)
throws java.io.IOException {
java.io.FileOutputStream fos = null;
java.io.BufferedInputStream bis = null;
try {
bis = getstream(fileUrl);
java.io.File file_in = new java.io.File(localFile);
fos = new java.io.FileOutputStream(file_in);
byte[] bytes = new byte[4096];
int c;
while ((c = bis.read(bytes)) != -1) {
fos.write(bytes, 0, c);
}
} finally {
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
}
}
if (bis != null) {
try {
bis.close();
} catch (Exception e) {

}
}
}
}
private static java.io.BufferedInputStream getstream(String fileUrl)
throws java.io.IOException {
java.io.BufferedInputStream bis;
java.net.URL url1 = new java.net.URL(fileUrl);
java.net.HttpURLConnection huc = (java.net.HttpURLConnection) url1
.openConnection();

bis = new java.io.BufferedInputStream(huc.getInputStream());
return bis;
}
...全文
62 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahaqzylx3_4 2017-01-20
  • 打赏
  • 举报
回复
就是资源路径
旅人葉 2017-01-20
  • 打赏
  • 举报
回复
引用 1 楼 ahaqzylx3_4 的回复:
public class testMethod { //@Test public void testlengthAndWidth() throws FileNotFoundException, IOException{ //获取网络图片 String path="http://********/20170820/f3e72af61b265dbfd7e8edb79a716796.swf"; //new 一个URL对象 URL url=new URL(path); //打开连接 HttpURLConnection conn=(HttpURLConnection) url.openConnection(); //设置get请求方式 conn.setRequestMethod("GET"); //设置超时响应时间为5秒 conn.setConnectTimeout(5*1000); //通过输入流获取图片数据 InputStream inStream=conn.getInputStream(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); //创建一个Buffer字符串 byte[] buffer = new byte[1024]; //每次读取的字符串长度,如果为-1,代表全部读取完毕 int len = 0; //使用一个输入流从buffer里把数据读取出来 while( (len=inStream.read(buffer)) != -1 ){ //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 outStream.write(buffer, 0, len); } //关闭输入流 inStream.close(); //把outStream里的数据写入内存 //得到图片二进制数据,以二进制封装得到数据,具有通用性 byte[] data=outStream.toByteArray(); //new一个文件对象用来保存图片,默认保存当前工程根目录 File imageFile = new File("E:\\temp\\mime\\BeautyGirl.jpg"); //创建输出流 FileOutputStream outStream1 = new FileOutputStream(imageFile); //写入数据 outStream1.write(data); //关闭输出流 outStream1.close(); }
也就是说那个fileUrl应该填入的是目标文件的网络路径是么,,,
ahaqzylx3_4 2017-01-20
  • 打赏
  • 举报
回复
URL 可以是接口地址,也可以是其他网络文件
ahaqzylx3_4 2017-01-20
  • 打赏
  • 举报
回复
public class testMethod {
//@Test
public void testlengthAndWidth() throws FileNotFoundException, IOException{
//获取网络图片
String path="http://********/20170820/f3e72af61b265dbfd7e8edb79a716796.swf";
//new 一个URL对象
URL url=new URL(path);
//打开连接
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
//设置get请求方式
conn.setRequestMethod("GET");
//设置超时响应时间为5秒
conn.setConnectTimeout(5*1000);
//通过输入流获取图片数据
InputStream inStream=conn.getInputStream();

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
//使用一个输入流从buffer里把数据读取出来
while( (len=inStream.read(buffer)) != -1 ){
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
//把outStream里的数据写入内存
//得到图片二进制数据,以二进制封装得到数据,具有通用性
byte[] data=outStream.toByteArray();
//new一个文件对象用来保存图片,默认保存当前工程根目录
File imageFile = new File("E:\\temp\\mime\\BeautyGirl.jpg");
//创建输出流
FileOutputStream outStream1 = new FileOutputStream(imageFile);
//写入数据
outStream1.write(data);
//关闭输出流
outStream1.close();
}

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧