在两台web主机间传输图片,发送端正常输出,接收端收不到数据,提示"input.read()=-1",没读到值吗?难道方法错误了吗?
帮忙看看,急
发送端:
public void sendImages(){
try {
String  filepath = "E:\\123.bmp";
URL serverURL = new URL("http://192.168.0.5:8080/web/ReceiveImagesServlet");
HttpURLConnection httpConn = (HttpURLConnection) serverURL.openConnection();
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
FileInputStream fInputS = new FileInputStream(filepath);
BufferedInputStream bInputS = new BufferedInputStream(fInputS,Contans.SendFileBufferSize);
byte buf[] = new byte[1024];
int length =-1 ;
try {
while((length = bInputS.read(buf,0,Contans.SendFileBufferSize)) != -1)
out.write(length);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e) {
e.printStackTrace();
return;
}
}
接收端:
public class ReceiveImagesServlet extends HttpServlet {
    public void init() throws ServletException {
    }
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response) throws IOException,
            ServletException {         
       InputStream input = request.getInputStream();
String filePath = "D:\\upload\\1.jpg";  
FileOutputStream fOutputS = new FileOutputStream(filePath);
BufferedOutputStream bOutputS = new BufferedOutputStream(fOutputS);

byte buf[] = new byte[1024];
int length;
System.out.println("input.read():"+input.read());//为什么这里的"input.read()=-1",没读到值吗?
while((length = input.read()) != -1){
bOutputS.write(length);
bOutputS.flush();
bOutputS.close();
}

    }    public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws IOException,
            ServletException {
        doPost(request, response);
    }
    public void response(HttpServletResponse response, String resStr) throws
            IOException {
        
        response.setContentType("text/xml; charset=GBK");
        response.getWriter().write(resStr);
        response.getWriter().flush();
        response.getWriter().close();
    }
    public void destroy() {
    }
}

解决方案 »

  1.   

    你这是个上传的过程   HttpURLConnection可以上传吗?  没试过  不过可以下载不知道你说的"两台web主机"是什么意思?  两台服务器?
    你上面的操作反过来是可以的,由HttpURLConnection提交请求  服务器写图片  客户端接收图片
      

  2.   

     是两台服务器,工程部署在两个tomcat下,我现在要做的是客户端发送图片,服务器接收图片。
      

  3.   

    即能是两台服务器,返过来也是一样的吗,以其中一台为服务器端  另一台为客户端服务器端:
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        bis = new BufferedInputStream(new FileInputStream(new File("图片")));
        bos = new BufferedOutputStream(response.getOutputStream());
        int n = 0;
        while((n=bis.read()) != -1) {
            bos.write(n);
        }
    }finally {
        if(bis != null) bis.close();
        if(bos != null) bos.close();
    }
    客户端:
    URL url = new URL("对应服务器的请求方法");
    HttpURLConnection httpConn = null;
    BufferedInputStream bis = null ;
    BufferedOutputStream bos = null;
    try {
        httpConn = (HttpURLConnection)url.openConnection();
        if(httpConn.getResponseCode() == 200) {
    bis = new BufferedInputStream(httpConn.getInputStream());
    bos = new BufferedOutputStream(new FileOutputStream(new File("图片保存位置"))); 
    int n = 0;
    while((n=bis.read()) != -1) {
        bos.write(n);
    }
        }
    }finally {
        if(bis != null) bis.close();
        if(bos != null) bos.close();
    }
      

  4.   

    你把我的代码直接考到你电脑上运行注意:httpConn.getResponseCode()  这句不能少   这句就表示向服务器提出请求
      

  5.   

    我的程序是读取图片列表,然后在要发送的图片前面打勾,填上服务端IP地址后,发送。
    服务端有个ReceiveImagesServlet 接收,自动保存到tomcat下的一个upload文件下。
    现在要如何做呢?谢谢。图片列表已经取出,就是发送出问题了。
      

  6.   

    服务器端是个servlet是吗?  那可以用SmartUpload,不过SmartUpload上传速度相当慢!如果你用Struts的话,他上传是非常快的
      

  7.   

    是servlet,呵呵,SmartUpload,可以吗?
      

  8.   

    是用应用程序上传? 还是jsp上传?