我是这样做的我尝试了所有的东西,但它不起作用,这是我的问题, 我正在尝试将一些文件从一台机器发送到另一台机器,想法是在目标机器上创建一个servlet,在服务器上运行它并等待任何客户机连接,客户机和servlet代码完美地工作在一个局域网类型,但是当涉及到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
答案 0 :(得分:0)
也许您误解了网络地址的工作原理。
192.168.1.12:8080是来自专用网络地址池的地址。您应该使用您的外部地址(可以通过例如http://ipaddress.com/获得)。但并非所有ISP都提供外部地址(也称为公共IP)。