在两台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() {
}
}
帮忙看看,急
发送端:
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() {
}
}
你上面的操作反过来是可以的,由HttpURLConnection提交请求 服务器写图片 客户端接收图片
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();
}
服务端有个ReceiveImagesServlet 接收,自动保存到tomcat下的一个upload文件下。
现在要如何做呢?谢谢。图片列表已经取出,就是发送出问题了。