想发送一个音频文件,做法是在jsp里把路径获取,然后点击发送,调用servlet进行发送。
jsp里的:
       <form action="audiopubish" method="post" enctype="multipart/form-data">
            <input type="file" name="audiopath" size="45" >
            <input type="submit" value="发送"><br><br>
        </form>
servlet里的:
import java.io.BufferedInputStream;
import java.io.IOException;
//import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.StreamMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class audiopublish {
        String msg=request.getParameter("audiopath");
        if(audiopath.length()==0)
            { response.sendRedirect("index.jsp");}
        else
            {
    public static String FILE_NAME = msg;    public static void main(String[] args) throws JMSException, IOException {
        ConnectionFactory factory = new ActiveMQConnectionFactory("failover://(tcp://localhost:61616)");
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createTopic("EXCHANGE.FILE");
        MessageProducer producer = session.createProducer(destination);
        long time = System.currentTimeMillis();        StreamMessage message = session.createStreamMessage();
        message.setStringProperty("COMMAND", "start");
        producer.send(message);
        byte[] content = new byte[4096];
        int byteread = 0;
           String strfile;//for test output
        File file = new File(FILE_NAME);
        FileInputStream fis = new FileInputStream(file);
        //  InputStream ins = docPublisher.class.getResourceAsStream(FILE_NAME);//unuse
        BufferedInputStream bins = new BufferedInputStream(fis);
        while ((byteread = bins.read(content)) != -1) {            message = session.createStreamMessage();
            message.setStringProperty("FILE_NAME", FILE_NAME);
            message.setStringProperty("COMMAND", "sending");
            message.clearBody();
            message.writeBytes(content);
            producer.send(message);
             strfile=new String(content,0,byteread);//for test output
              System.out.println(strfile);//for test output        }
        bins.close();
        //  ins.close();//unuse
        message = session.createStreamMessage();
        message.setStringProperty("COMMAND", "end");
        producer.send(message);        connection.close();
        System.out.println("Total Time costed : " + (System.currentTimeMillis() - time) + " mili seconds");
    }
}问题是:如果不让jsp给servlet传值,即:删了
String msg=request.getParameter("audiopath");
        if(audiopath.length()==0)
            { response.sendRedirect("index.jsp");}
        else
            {}
直接把 msg 赋值,比如"test.mp3",这样运行servlet是正常的。但是现在:  
request和response提示找不到符号,
if(audiopath.length()==0)提示非法的类型开始,软件包audiopath不存在,
sendRedirect("index.jsp");}提示缺少返回语句
else 提示非法的类型开始public static String FILE_NAME = msg; 提示非法的表达式开始
求解啊本人小白。

解决方案 »

  1.   

    audiopath怎么可以直接引用。。你没有他又不是你java中的属性
      

  2.   


    请问,那我应该怎么把这个audiopath引到servlet里面来呢
      

  3.   

    String msg=request.getParameter("audiopath");这个就是得到audiopath。将audiopath替换成msg。或者添加audiopath的getter方法。getAudiopath()
     
      

  4.   

    因为servlet里要用request的时候,要先声明,也可以直接放在doget这样的函数里