怎么样获取URL中的文件

lh572945417 2016-08-30 11:52:54

就是获取这个url的文件,文件是在服务器上的
...全文
354 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
江城老金 2016-08-31
  • 打赏
  • 举报
回复
在浏览器里面打开 然后另存为就可以了
lmkght 2016-08-30
  • 打赏
  • 举报
回复
引用 13 楼 lh572945417 的回复:
[quote=引用 12 楼 lmkght 的回复:] 如果中文乱码的话把InputStreamReader isr = new InputStreamReader(is); 改成 InputStreamReader isr = new InputStreamReader(is,"utf-8");
这是读取文件,获得文件也是这个吗[/quote] InputStream is = url.openStream();这个是获得
lh572945417 2016-08-30
  • 打赏
  • 举报
回复
引用 12 楼 lmkght 的回复:
如果中文乱码的话把InputStreamReader isr = new InputStreamReader(is); 改成 InputStreamReader isr = new InputStreamReader(is,"utf-8");
这是读取文件,获得文件也是这个吗
lmkght 2016-08-30
  • 打赏
  • 举报
回复
如果中文乱码的话把InputStreamReader isr = new InputStreamReader(is); 改成 InputStreamReader isr = new InputStreamReader(is,"utf-8");
lmkght 2016-08-30
  • 打赏
  • 举报
回复
这个文件既然在服务器上为什么还要用url?
引用 10 楼 lh572945417 的回复:
[quote=引用 9 楼 lmkght 的回复:] 如果是单纯的读内容,可以试试这个简单的 File file = new File(“地址”); try { String data = FileUtils.readFileToString(file, “utf-8"); } catch (IOException e) { e.printStackTrace(); }
URL也是直接用吗[/quote] String str; try { URL url = new URL("http://192.168.1.11/dp/dat/bb.txt"); InputStream is = url.openStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); while((str = br.readLine()) != null) System.out.println(str); br.close(); } catch(IOException e) { System.out.println(e); } 这个方法可以 我测试过了
lh572945417 2016-08-30
  • 打赏
  • 举报
回复
引用 9 楼 lmkght 的回复:
如果是单纯的读内容,可以试试这个简单的 File file = new File(“地址”); try { String data = FileUtils.readFileToString(file, “utf-8"); } catch (IOException e) { e.printStackTrace(); }
URL也是直接用吗
lmkght 2016-08-30
  • 打赏
  • 举报
回复
如果是单纯的读内容,可以试试这个简单的 File file = new File(“地址”); try { String data = FileUtils.readFileToString(file, “utf-8"); } catch (IOException e) { e.printStackTrace(); }
lh572945417 2016-08-30
  • 打赏
  • 举报
回复
引用 7 楼 lmkght 的回复:
你这个url在地址栏直接打开可以看到内容么
可以的
lmkght 2016-08-30
  • 打赏
  • 举报
回复
你这个url在地址栏直接打开可以看到内容么
lh572945417 2016-08-30
  • 打赏
  • 举报
回复
引用 3 楼 Java_Gump 的回复:
response.setCharacterEncoding("UTF-8");
		response.setContentType("application/pdf;charset=utf-8");
		FileInputStream in = new FileInputStream(new File(downLoadAgreementService.resultPath(request)));
		OutputStream out = response.getOutputStream();
		byte[] b = new byte[512];
		while ((in.read(b)) != -1) {
			out.write(b);
		}
		out.flush();
		in.close();
		out.close();
把file路径替换一下
好像不行呀
lh572945417 2016-08-30
  • 打赏
  • 举报
回复
引用 4 楼 shijing266 的回复:
[quote=引用 2 楼 lh572945417 的回复:] [quote=引用 1 楼 shijing266 的回复:] 通过文件流去下载呢?
不是下载,就是想获取文件[/quote] 一样的,文件流去读取远程文件: 参考[/quote] 读的文件我就可以对他进行操作了吗
  • 打赏
  • 举报
回复
引用 2 楼 lh572945417 的回复:
[quote=引用 1 楼 shijing266 的回复:] 通过文件流去下载呢?
不是下载,就是想获取文件[/quote] 一样的,文件流去读取远程文件: 参考
Java_Gump 2016-08-30
  • 打赏
  • 举报
回复
response.setCharacterEncoding("UTF-8");
		response.setContentType("application/pdf;charset=utf-8");
		FileInputStream in = new FileInputStream(new File(downLoadAgreementService.resultPath(request)));
		OutputStream out = response.getOutputStream();
		byte[] b = new byte[512];
		while ((in.read(b)) != -1) {
			out.write(b);
		}
		out.flush();
		in.close();
		out.close();
把file路径替换一下
lh572945417 2016-08-30
  • 打赏
  • 举报
回复
引用 1 楼 shijing266 的回复:
通过文件流去下载呢?
不是下载,就是想获取文件
  • 打赏
  • 举报
回复
通过文件流去下载呢?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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