[英]Exception in thread “main” org.apache.http.conn.HttpHostConnectException: Connect to // failed: Connection timed out: connect
i'm so done i tried everything thing but it doesn't work, here is my problem, I'm trying to send some files from a machine to another, the idea is to create a servlet on the destination machine, run it on server and waiting for any clients to be connected with, the client and the servlet code works perfectly in a lan network type, but when it comes to a wan network type, i get this exception 我已经做完了,我尝试了所有事情,但是没有用,这是我的问题,我正在尝试将某些文件从一台计算机发送到另一台计算机,这个想法是在目标计算机上创建一个servlet,然后在该计算机上运行服务器并等待任何客户端与之连接,客户端和servlet代码在lan网络类型中可以完美工作,但是当涉及wan网络类型时,我会遇到此异常
Exception in thread "main" org.apache.http.conn.HttpHostConnectException: Connect to 192.168.1.12:8080 [/192.168.1.12] failed: Connection timed out: connect 线程“主” org.apache.http.conn.HttpHostConnectException中的异常:连接到192.168.1.12:8080 [/192.168.1.12]失败:连接超时:连接
these are consecutively the client and the servlet code 这些依次是客户端和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();
}
}
}
and this is the servlet code 这是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
Maybe you misunderstood how network addresses works. 也许您误解了网络地址的工作原理。
192.168.1.12:8080 is an address from Private Network Address Pool. 192.168.1.12:8080是来自专用网络地址池的地址。 You should use your's external address (can be obtained by eg http://ipaddress.com/ ). 您应该使用您的外部地址(可以通过例如http://ipaddress.com/获得)。 But not all ISP provides external address (called also as public IP). 但是,并非所有ISP都提供外部地址(也称为公共IP)。
http://en.wikipedia.org/wiki/Private_network http://en.wikipedia.org/wiki/Private_network
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.