`
me-
  • 浏览: 69452 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java http下载文件。在activemq blobmessage 的blob对象读时用到。

阅读更多

http://stackoverflow.com/questions/401748/corrupt-file-when-using-java-to-download-file

 

 

错误代码
URL targetUrl = new URL(urlForFile);
InputStream content = (InputStream)targetUrl.getContent();
BufferedInputStream buffered = new BufferedInputStream(content);
File savedFile = File.createTempFile("temp",".dat");

FileOutputStream fos = new FileOutputStream(savedFile);
int letter;
while((letter = buffered.read()) != -1)
fos.write(letter);
fos.close();

 

 

三大问题:

  1. You're not just treating the input as bytes,未彻底二进制处理
  2. You're needlessly pulling the entire object into memory at once,性能考虑
  3. You're doing multiple method calls for every single byte read and written -- use the array based read/write!,性能考虑

Here's a redo,经过验证的正确处理图片的代码。:

URL targetUrl = new URL(urlForFile); 
InputStream is = targetUrl.getInputStream(); 
File savedFile = File.createTempFile("temp",".dat"); 
FileOutputStream fos = new FileOutputStream(savedFile); 
 
int count; 
byte[] buff = new byte[16 * 1024]; 
while((count = is.read(buff)) != -1) { 
    fos.write(buff, 0, count); 
} 
fos.close(); 
content.close(); 
分享到:
评论

相关推荐

    在Java与C++间应用Activemq

    RetailSystem包含消息生产者Producer,BankSystem包含消息接收者Consumer和Java调用C++的中间类DLL,dll3为Java方法在C++下的实现.

    java操作activeMQ(java项目代码及jar包可运行,队列和订阅模式)

    java操作activeMQ(java项目代码及jar包可运行,队列和订阅模式)

    java springboot整合activemq工程

    java springboot整合activemq工程 #activemq配置 #默认情况下activemq提供的是queue模式 true是可以使用topic,false是仅使用queue模式 spring.jms.pub-sub-domain: true # 设置连接的activemq服务器 spring....

    activeMQ在文件上传的应用

    activeMQ与uploadify整合

    activeMQ的java简单实例.zip

    activeMQ的java简单实例.zipactiveMQ的java简单实例.zip

    测试activeMQ的java程序

    activeMQ是jms的一种,是java是实现两个系统之间交互的方式,MQ分为队列模式和订阅模式,对这两种模式分别进行了测试通过。

    activemq, Apache ActiveMQ镜像.zip

    activemq, Apache ActiveMQ镜像 欢迎来到 Apache ActiveMQis是一个高性能的Apache 2.0许可以消息代理和 JMS 1.1实现。正在启动要帮助你入门,请尝试以下链接:入门http://activemq.apache.org/version-

    ActiveMQ-Java P2P模式MQ实战

    去官方网站下载:http://activemq.apache.org/ 我下载的时候是 ActiveMQ 5.14.4 Release版 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信...

    java消息中间件教程-activemq

    主要讲解activemq的安装,使用,集群的搭建,以及拓展

    ActiveMQ JAVA简单项目案例

    ActiveMQ 是Apache出品,最流行的,能力...本压缩包是是实现ActiveMQ中点对点与发布订阅模式的java项目,下载后可配合运行好的ActiveMQ服务直接运行。 没有ActiveMQ服务的可下载本人的ActiveMQ消息中间件,运行即可。

    ActiveMQ 5 java 源码

    ActiveMQ 5 java 源码 直接从svn checkout 下来的代码。

    java中间件之activemq

    ActiveMQ 是由 Apache 出品的一款开源消息中间件,旨在为应用程序提供高效、可扩展、稳定、安全的企业级消息通信。它的设计目标是提供标准的、面向消息的、多语言的应用集成消息通信中间件。ActiveMQ 实现了 JMS 1.1...

    JAVA编程之Spring-activeMQ基础开发

    2 在项目中,我们为消息的生产者和发布者分别注册了两个消费者和订阅者,当有消息到达activeMQ时,消费者和订阅者会自动获取对应的消息,其中两个消费者会轮流消费消息,而两个订阅者会同时订阅所有消息;...

    activeMQ 服务端客户端 java代码

    activeMQ 服务端客户端 java代码 activeMQ 服务端客户端 java代码

    activemq + spring

    包含两个功能 1.使用JAVA连接activemq 2.使用spring整合activemq

    ActiveMQ5.13 安装与配置

    在解压缩 ActiveMQ 安装包时,需要指定解压缩的目录。可以根据需要指定不同的目录。 在配置环境变量时,需要重新加载 `/etc/profile` 文件,以便生效。 在启动 ActiveMQ 之前,需要确保环境变量已经配置完成。 ...

    activemq.zip

    ActiveMQ安装包及安装说明文档,适合于初次安装ActiveMQ的新手,包含java及springboot调用ActiveMQ的jar包依赖!

    JAVA上百实例源码以及开源项目

     在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息的内容。同时一个mail note将被发送给消息发送者,发送一个e-mail通知给由recipient参数确定的e-...

    activemq-web-console-5.11.2

    activemq-web-console的默认使用方式是通过在activemq.xml中导入jetty.xml配置一个jetty server来实现的...3.还有一个fileserver,用来支持通过activemq发送文件时的中转服务器。blob message时配置的http文件服务器。

    activemq-demo

    一个用Spring+Activemq实现的消息平台

Global site tag (gtag.js) - Google Analytics