package test; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.InputStream; 
import javax.jms.Destination; 
import javax.jms.JMSException; 
import javax.jms.MessageProducer; 
import javax.jms.QueueConnection; 
import javax.jms.QueueConnectionFactory; 
import javax.jms.QueueSession; 
import javax.jms.Session; 
import org.apache.activemq.ActiveMQConnection; 
import org.apache.activemq.ActiveMQConnectionFactory; 
import org.apache.activemq.ActiveMQSession; 
import org.apache.activemq.BlobMessage; 
import org.apache.activemq.command.ActiveMQBlobMessage; 
import org.apache.activemq.command.ActiveMQQueue; public class Sending { 
/** 
  * @param args 
  * @throws IOException 
  */ 
public static void main(String[] args) throws IOException { 
  // TODO Auto-generated method stub 
  long a1 = System.currentTimeMillis(); 
  
  /* 
  * First you must tell how the Blob repository can be found. 
  * Use ActiveMQ 5.1.0+ as broker because the fileserver webapp works there out of the box. 
  */ 
  ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory( 
    "tcp://localhost:61616?jms.blobTransferPolicy.defaultUploadUrl=http://localhost:8686/fileserver/"); 
  ActiveMQConnection conn = null; 
  ActiveMQSession session = null; 
  try { 
  conn = (ActiveMQConnection)cf.createConnection(); 
  conn.start(); 
  session = (ActiveMQSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE); 
  Destination destination = new ActiveMQQueue("Queue"); 
  MessageProducer producer = session.createProducer(destination); 
    
  /* 
    * !!!!!!!!!!!!!!!!!!!!!!!!! 
    * very important. If it is set to true (default) the uploader is lost in translation ;) 
    * !!!!!!!!!!!!!!!!!!!!!!!!! 
    */ 
  conn.setCopyMessageOnSend(true); 
    
  File file = File.createTempFile("amq-data-file-", ".dat"); 
  // lets write some data 
  BufferedWriter writer = new BufferedWriter(new FileWriter(file)); 
  writer.append("Hello World!"); 
  writer.close(); 
    
  /* 
    * i have used an simple ascii file instead of the picture you want to use 
    */ 
  BlobMessage message = session.createBlobMessage(file); 
    
  /* 
    * should only work if you receive the message 
    */ 
  System.out.println(message.getInputStream()); 
    
  producer.send(message); 
    
  /* 
    * After all you can see a new file under $Brokerlocation$/webapps/fileserver/ 
    */ 
  } catch (JMSException e) { 
  e.printStackTrace(); 
  } finally { 
  try { 
    if (session != null) { 
    session.close(); 
    } 
    if (conn != null) { 
    conn.close(); 
    } 
  } catch (JMSException ex) { 
  } 
  } 
  a1 = System.currentTimeMillis() - a1; 
  System.out.println(a1); 


报错: 
javax.jms.JMSException: PUT was not successful: 403 Forbidden 

解决方案 »

  1.   

    你确定除了传blobmessage,其它message可以么?
      

  2.   

    javax.jms.JMSException: PUT was not successful: 403 Forbidden 好像server端访问不了哟。
      

  3.   

    我用apache-activemq-5.2.0测试了你的代码,在我机器上很OK
    不是代码的问题,应该是你的设置问题....检查一下以下配置        <!-- The transport connectors ActiveMQ will listen to -->
            <transportConnectors>
                <transportConnector name="openwire" uri="tcp://localhost:61616" discoveryUri="multicast://default"/>
                <transportConnector name="ssl" uri="ssl://localhost:61617"/>
                <transportConnector name="stomp" uri="stomp://localhost:61613"/>
                <transportConnector name="xmpp" uri="xmpp://localhost:61222"/>还有
        <jetty xmlns="http://mortbay.com/schemas/jetty/1.0">
            <connectors>
                <nioConnector port="8161"/>
            </connectors>        <handlers>
                <webAppContext contextPath="/admin" resourceBase="${activemq.base}/webapps/admin" logUrlOnStart="true"/>
                <webAppContext contextPath="/demo" resourceBase="${activemq.base}/webapps/demo" logUrlOnStart="true"/>
                <webAppContext contextPath="/fileserver" resourceBase="${activemq.base}/webapps/fileserver" logUrlOnStart="true"/>
            </handlers>
        </jetty>
      

  4.   

    如果你用它自带的activemq.bat启动,是没有错误的,但是用tomcat启动,是不行的。
      

  5.   

    不知道你说的 “用tomcat启动”是怎么一个启动发,能较一下我吗?LZ
      

  6.   

    activemq是什么东东??看完贴我要去google一下