用户在一台机器中登陆服务器A的应用系统,通过
<html:form action="/upload" enctype="multipart/form-data" method="post">
<input name="file" id="file" type="file" onChange="setPic1()">
<input type="submit" name="Submit" value="提 交"> 
</html:form>
把图片上传到服务器A,但是这个服务器A对这张传来的图片不作处理,而是把它转发到另外一台服务器B上(如IP为:10.111.52.170,Tomcat端口为8080),服务器B把接收到的流保存为文件后发给服务器A一个结果状态信息,服务器A再把一个结果状态发给用户。我以下的方法在服务器B中为什么图片没有预览而在服务器A中图片正常,急啊,请高手相助!!服务器A主类:UpAction.java:public class UpAction extends Action { private static final String SERVER_URL = "http://10.142.50.170:8080/wfxtimgserver/ServerServlet"; public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UpForm upForm = (UpForm) form;
saveUpFile(upForm.getFile());//org.apache.struts.upload.FormFile对象
InputStream stream = ImageOpe.getTextImageByInputSAndTransfer("test1.jpg");
try {
processRequest(request,response,stream);
} catch (ServletException e) {

} catch (IOException e) {

}
return null;
} protected void processRequest(HttpServletRequest request,
HttpServletResponse response, InputStream stream) throws ServletException, IOException {
String url2 = SERVER_URL;
URL url = new URL(SERVER_URL);
         URLConnection connection = url.openConnection();
          connection.setDoInput(true);
         connection.setDoOutput(true);
         connection.setUseCaches(false);
         connection.setDefaultUseCaches(false);
         connection.setRequestProperty("Content-Type", "application/octet-stream");
         BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
         String input = null;
         ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
         int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
oos.write(buffer, 0, bytesRead);
}
         oos.flush();
        ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());
         try {
System.out.println("UpAction " + ois.readObject());
} catch (IOException e) {

} catch (ClassNotFoundException e) {

}
}

public InputStream saveUpFile(FormFile upfile) {
try {
InputStream stream = upfile.getInputStream();
OutputStream bos = new FileOutputStream("test1.jpg");
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
return null;
}
}服务器B:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doPost wfxtimgserver");
InputStream stream = request.getInputStream();
OutputStream bos = new FileOutputStream("test2.jpg");
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject("OK");
oos.flush();
}

解决方案 »

  1.   

    可以在A中发送一个HTTP请求,然后B中根据这个请求的参数取得图片,成功则可以返回(输出)OK给A,失败返回其他给A,A根据返回状态码另做处理。
      

  2.   

    从A中传图片URL给B后,B可以这样处理图片,然后返回状态报告给A。
               try{
                URL url = new URL("YourResourceUrl");
                System.out.println("Opening connection to " + YourResourceUrl + "...");
                URLConnection urlC = url.openConnection();
                InputStream is = url.openStream();
                FileOutputStream   fos = new FileOutputStream("YourFileName");//这里要给保留下来的图片取个名字,可以根据参数取得。
                for(count = 0; (oneChar = is.read()) != -1; count++)
                fos.write(oneChar);
                is.close();
                fos.close();
                out.print("OK");
                 } catch(Exception ex){
                 out.print("Faild");
                 }
      

  3.   

    楼上的
    我已经在A中发送一个HTTP请求了呀,就是把这个图片转换成流传给B,但是在B中生成图片时没有预览,也就是流错误了,就是不知道错在哪,洗耳听您指导!!
      

  4.   

    不知道你可以不可以像我的例子那样,直接用http 的get请求传个图片的url给B,然后B根据这个URL再取图片?
    我现在就是在项目中用这中方法,一直正常。
      

  5.   

    这样也可以达到效果,但是这会带来数据冗余,就是AB服务器都存储了相同的图片,当图片量很大是,对A就很不好了,我想要的最理想的效果是:B只是收到图片流就存,相当于Webservice,A只是负责把客户端传来的图片转为流再传给B,当A收到B发回的结果再作其他的处理。
    不管怎样,还是忠心感谢你。
    现在还不能结贴,等结了请你接分。