2012-03-08 94 views
2

我想从客户端上传文件到服务器。如何处理从Java客户端上传的服务器端HTTP文件

客户端:Java中使用HTTP发布 服务器:Java Servlet的

我已经添加了客户端在这里编码。但是,我不知道服务器端处理。请用代码片段帮助我。

private String Tag = "UPLOADER"; 
    private String urlString = "http://192.168.42.140:8080/sampweb"; 
    HttpURLConnection conn; 
    String exsistingFileName = "/sdcard/log.txt"; 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    try { 
     // ------------------ CLIENT REQUEST 

     Log.e(Tag, "Inside second Method"); 

     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); 

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

     dos.writeBytes(twoHyphens + boundary + lineEnd); 
     dos 
       .writeBytes("Content-Disposition: post-data; name=uploadedfile;filename=" 
         + exsistingFileName + "" + lineEnd); 
     dos.writeBytes(lineEnd); 

     Log.e(Tag, "Headers are written"); 

     // create a buffer of maximum size 

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

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

     int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable); 

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

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

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

     // close streams 
     Log.e(Tag, "File is written"); 
     fileInputStream.close(); 
     dos.flush(); 
     dos.close(); 

    } catch (MalformedURLException ex) { 
     Log.e(Tag, "error: " + ex.getMessage(), ex); 
    } 

    catch (IOException ioe) { 
     Log.e(Tag, "error: " + ioe.getMessage(), ioe); 
    } 

    try { 
     BufferedReader rd = new BufferedReader(new InputStreamReader(conn 
       .getInputStream())); 
     String line; 
     while ((line = rd.readLine()) != null) { 
      Log.e("Dialoge Box", "Message: " + line); 
     } 
     rd.close(); 

    } catch (IOException ioex) { 
     Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex); 
    } 
} 

我是新来的服务器编程。如果我犯了什么错误,请澄清我。谢谢!

+0

标题有误导性,但问题本身在试图在Java中实现* server *端而不是在客户端上很明显。当前链接的副本描述客户端实现。 – 2016-02-03 15:06:02

回答

-1

达到此目的的最佳图书馆仍然是Apache Commons File Upload。它是有据可查的,易于使用。如果遇到任何问题,请首先检查FAQ

+1

这不起作用。 Commons File Upload用于服务器而不是客户端。 – 2015-08-23 20:11:02

+0

问题是:*“我在这里添加了客户端编码,但是,我对服务器端处理没有任何想法。”* – 2015-08-24 04:41:23

+0

@GergelyBasco,如果您阅读标题,则问题还不清楚。真正的问题已经减少到一句话。感谢指针。 – 2015-08-24 07:13:27

相关问题