当前位置: 首页 > 知识库问答 >
问题:

线程“main”org.apache.http.conn.HTTPhostConnectException:连接到//失败:连接超时:连接

费朗
2023-03-14

我已经做了所有的尝试,但都不起作用,这是我的问题,我试图将一些文件从一台机器发送到另一台机器,想法是在目标机器上创建一个servlet,在服务器上运行它,并等待任何客户机连接,客户机和servlet代码在lan网络类型中完美地工作,但当涉及到wan网络类型时,我得到了这个例外

线程“main”org.apache.http.conn.HTTPhostConnectException:连接到192.168.1.12:8080[/192.168.1.12]失败:连接超时:连接

它们依次是客户机和servlet代码

import java.io.File;

    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;

    public class UploadClient
    {

public static void main(String[] args) throws Exception 
{
    String test="C:/Users/Soufiane/Desktop/Dossier/";
    if (test.equalsIgnoreCase("")) 
    {
        System.out.println("Files path not given");
        System.exit(1);
    }

    CloseableHttpClient httpclient = HttpClients.createDefault();
    try
    {

          // On cree une connection avec la servlet qui va recevoir les fichiers
      HttpPost httppost = new HttpPost("http://192.168.1.12:8080/soufiane/haha");

   MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
   FileBody bin;

   // le nom du chemin de repertoire contenant les fichiers à tranmettre doit s   terminer par un "\". 
        if(test.endsWith("/"))
        {
            File file = new File(test);
            String[] files = file.list();
            for(int i=0;i<files.length;i++)
            {
                bin = new FileBody(new File(test + files[i]));
                multipartEntityBuilder.addPart("bin_"+i, bin);
            }
        }
        // si le nom du chemin de repertoire contenant les fichiers à tranmettre ne se terminer par un "\" alors on 
        // considere que c'est un nom qui correspond à un fichier à transmettre seul.
        else
        {
            bin = new FileBody(new File(test));
            multipartEntityBuilder.addPart("bin", bin);
        }

        HttpEntity reqEntity = multipartEntityBuilder.build();
        httppost.setEntity(reqEntity);

        System.out.println("executing request " + httppost.getRequestLine());

        CloseableHttpResponse response = httpclient.execute(httppost);

        try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    System.out.println("Response content length: "
                            + resEntity.getContentLength());
                }
                System.out.println("Response result: "+EntityUtils.toString(resEntity));

                //  EntityUtils.consume(resEntity);

        }
        finally 
        {
            response.close();
        }

        } 
        finally 
        {
         httpclient.close();
        }
       }
      }

这是servlet代码

                import javax.servlet.http.HttpServlet;
                import java.io.File;
                 import java.io.IOException;
                  import java.io.PrintWriter;
              import java.util.Iterator;
             import java.util.List;
                   import javax.servlet.ServletException;
             import javax.servlet.http.HttpServletRequest;
             import javax.servlet.http.HttpServletResponse;
             import org.apache.commons.fileupload.FileItem;
             import org.apache.commons.fileupload.FileItemFactory;
              import org.apache.commons.fileupload.FileUploadException;
             import org.apache.commons.fileupload.disk.DiskFileItemFactory;
                import org.apache.commons.fileupload.servlet.ServletFileUpload;


    public class UploadServlet extends HttpServlet
    {
/**
 * 
 */
private static final long serialVersionUID = 1L;
      public UploadServlet() 
     {
    super();
}

/**
 * Destruction of the servlet. <br>
 */


 /**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("<HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println("<BODY>");
    out.print("This is ");
    out.print(this.getClass());
    out.println(", using the GET method");
    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close(); 
}

/*
 * The doPost method of the servlet. <br>

 * This method is called when a form has its tag value method equals to post.

 * @param request the request send by the client to the server.
 * @param response the response send by the server to the client.
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setSizeMax(1000000000);
    String filename="";
    String responseText = "";
    List<FileItem> fileItems;
    try 
    {
        fileItems = upload.parseRequest(request);
        // Process the uploaded items
        @SuppressWarnings("rawtypes")
        Iterator iter = fileItems.iterator();
        while (iter.hasNext())
        {
            FileItem item = (FileItem) iter.next();
            filename = item.getName();
            File file = new File("C:\\Users\\Soufiane\\Desktop"+filename);
            item.write(file);
       responseText = responseText+"\n"+"uploading file "+filename+" succeed";
         }
    } 
    catch (FileUploadException e) 
    {
        e.printStackTrace();
    } 
    catch (Exception e)
    {
        e.printStackTrace();
    } 
    finally 
    {
        if(responseText.isEmpty())
            responseText = "uploading files failed";
    }
    response.setContentType("text/html");
    response.setContentLength(responseText.length());
    PrintWriter out = response.getWriter();
    out.print(responseText);        
    out.flush();
    out.close();
}

/*
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occurs
 *
 *
 *
/*
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occurs
 */
    }

        i have an exame about this next monday, so plz help me

共有1个答案

壤驷阳冰
2023-03-14

也许你误解了网络地址是如何工作的。

192.168.1.12:8080是专用网络地址池中的地址。您应该使用您的外部地址(可以通过http://ipaddress.com/获得)。但并非所有ISP都提供外部地址(也称为公共IP)。

http://en.wikipedia.org/wiki/private_network

 类似资料:
  • 我一直在使用ftp在android应用程序的服务器上上传图像,我正在使用以下代码连接ftp。它在Wi-fi中工作正常,但如果我切换到3G或2G连接,就会出现连接超时错误。那么,你能告诉我如何处理这种情况吗。我的客户在Veriozon也面临这个问题,Sprint,ATT网络提供商。它的iPhone版本在所有网络中都运行良好。 代码: 错误: JAVA网ConnectException:无法连接到主机

  • 我知道,这个问题已经在不同的论坛上被问了很多次,但我仍然无法找到一个解决方案,这解决了我的问题...情况:我们在服务器上有一个nginx、php-fpm和MySQL堆栈。服务器位于nginx反向代理之后。问题是,在上游服务器上有干净的错误日志,而在反向代理上,我得到了多条消息 连接到>上游,客户端:+++++++++++,服务器:domain.com,请求:“Get>/files/imagecac

  • 我将kafka-connect docker映像()部署到ECS/fargate,为我的ECS服务分配了一个安全组,该安全组允许传入的zooper keeper和kafka bootstrap服务器通信量(纯文本和TLS),并分配了一个IAM角色,该角色允许我的ECS任务对MSK集群运行kafka操作,但connect集群在尝试从MSK集群获取代理列表时仍会超时。 在AWS中,kafka conn

  • Selenium Webdriver(2.53)使用testNG和java(jdk 8)。Mozilla Firefox(46)在页面打开时立即崩溃,并显示错误消息: org.apache.http.conn.连接到127.0.0.1:7055[/127.0.0.1]失败:连接拒绝:连接

  • 问题内容: 我在代码中使用了RMI: 这些是4个.java文件。 接下来,我编译所有这些文件。然后创建一个using 。之后,我使用来在服务器端启动rmi注册表。然后,我开始使用服务器,最后使用客户端。 但是什么也没发生 客户端抛出的异常是 原因是什么,我该如何解决? 在客户端计算机上,这些是以下.class文件,在服务器端 问题答案: 错误消息说明了一切:您的连接超时。这意味着您的请求在某个(默