在linux发布,无法下载图片

ice9ying 2017-05-11 11:13:32
在本地测试,下载网络图片到本地没有问题,但是发布到linux,却无法下载,但也没有报错。代码如下
  
URL url = new URL("网络图片地址");
// 打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
connection.setConnectTimeout(5 * 1000);
InputStream inputStream = connection.getInputStream();

byte[] bs = new byte[1024];
int len;
File sf=new File(exp_path); // /home/
if(!sf.exists()){
sf.mkdirs();
}
OutputStream os = new FileOutputStream(sf.getPath()+"\\123.png");
// 读取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}

os.flush();
// 关闭
os.close();
inputStream.close();


刚转做java,求各位高手相助
...全文
343 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ice9ying 2017-05-16
  • 打赏
  • 举报
回复
引用 2 楼 zhou2015bo 的回复:
OutputStream os = new FileOutputStream(sf.getPath()+"\\123.png");替换成OutputStream os = new FileOutputStream(sf.getPath()+"/123.png");
是这个问题没错,改成/就对了,虽然是改完以后才看到这里,但给分了。
Julywhj 2017-05-13
  • 打赏
  • 举报
回复
查看路径是否对了,或者Linux文件夹是否有写入和读取权限;
云墨浅殇 2017-05-12
  • 打赏
  • 举报
回复
OutputStream os = new FileOutputStream(sf.getPath()+"\\123.png");替换成OutputStream os = new FileOutputStream(sf.getPath()+"/123.png");
幽兔 2017-05-11
  • 打赏
  • 举报
回复
你确定LINUX已经连接到互联网了? 你用Linux的Firefox直接看看网络图片能看么?

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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