Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用HttpURLConnection将文件上载到XAMPP服务器_Java_Httpurlconnection - Fatal编程技术网

Java 使用HttpURLConnection将文件上载到XAMPP服务器

Java 使用HttpURLConnection将文件上载到XAMPP服务器,java,httpurlconnection,Java,Httpurlconnection,我正在尝试将文件上载到XAMPP服务器,但在服务器项目文件夹中看不到任何文件。但我看到字节写入已完成。我的代码中有什么问题。请帮助我 示例代码:: import java.io.*; import java.net.*; public class ServletCom { public static void main(String[] args) throws Exception { HttpURLConnection conn = null;

我正在尝试将文件上载到XAMPP服务器,但在服务器项目文件夹中看不到任何文件。但我看到字节写入已完成。我的代码中有什么问题。请帮助我

示例代码::

import java.io.*;
import java.net.*;

public class ServletCom {

    public static void main(String[] args)
      throws Exception
   {

      HttpURLConnection conn = null;
      BufferedReader br = null;
      DataOutputStream dos = null;
      DataInputStream inStream = null;

      InputStream is = null;
      OutputStream os = null;
      boolean ret = false;
      String StrMessage = "";
      String exsistingFileName = "C:\\Users\\hossain\\Desktop\\photo\\image\\text.txt";
      String fileName="text.txt";

      String lineEnd = "";
      String twoHyphens = "--";
      String boundary =  "*****";


  int bytesRead, bytesAvailable, bufferSize;

  byte[] buffer;

  int maxBufferSize = 1*1024*1024;


  String responseFromServer = "";

  String urlString = "http://local.ebajar/";


  try
  {
   //------------------ CLIENT REQUEST

   FileInputStream fileInputStream = new FileInputStream( new File(exsistingFileName) );

   // open a URL connection to the Servlet 

   URL url = new URL(urlString);


   // Open a HTTP connection to the URL

   conn = (HttpURLConnection) url.openConnection();

   // Allow Inputs
   conn.setDoInput(true);

   // Allow Outputs
   conn.setDoOutput(true);

   // Don't use a cached copy.
   conn.setUseCaches(false);

   // Use a post method.
   conn.setRequestMethod("POST");

   conn.setRequestProperty("Connection", "Keep-Alive");

   conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

   dos = new DataOutputStream( conn.getOutputStream() );

   dos.writeBytes(twoHyphens + boundary + lineEnd);
   dos.writeBytes("Content-Disposition: form-data; name=\"" + fileName + "\"" + lineEnd);
   //dos.writeBytes("Content-Disposition: form-data; name="upload";"+ " filename="" + exsistingFileName +""" + lineEnd);
   dos.writeBytes(lineEnd);



   // create a buffer of maximum size

   bytesAvailable = fileInputStream.available();
   bufferSize = Math.min(bytesAvailable, maxBufferSize);
   buffer = new byte[bufferSize];

   // read file and write it into form...

   bytesRead = fileInputStream.read(buffer, 0, bufferSize);

   while (bytesRead > 0)
   {
    dos.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
   }

   // send multipart form data necesssary after file data...

   dos.writeBytes(lineEnd);
   dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

   System.out.println("write complete........" );
   // close streams

   fileInputStream.close();
   dos.flush();
   dos.close();


  }
  catch (MalformedURLException ex)
  {
   System.out.println("From ServletCom CLIENT REQUEST:"+ex);
  }

  catch (IOException ioe)
  {
   System.out.println("From ServletCom CLIENT REQUEST:"+ioe);
  }
  catch (Exception e)
  {
   System.out.println("From ServletCom CLIENT REQUEST:"+e);
  }

   }
}

您需要一个脚本来接受服务器端的数据,例如类似upload.php的内容。我可以在服务器文件夹位置查看上载的文件吗?您编写的代码使用POST,似乎上载了内容类型为multipart/form data的内容。所以我想你想上传一个文件。是否要下载网页?您到底想实现什么?我想使用java客户端应用程序向ServerTomcat/xampp服务器写入一个文件。我想查看上传到项目文件夹路径的文件。对不起,我的英语不好。谢谢你的建议。我用ftp服务器和java客户端程序解决了我的问题。我认为ftp程序比HttpRlConnection客户端程序更好、更容易使用。