2016-03-02 112 views
0

我有一個使用multer來處理多部分POST請求的Node.js API。無法使用java將多部分帖子發送到node.js API服務器

我能夠成功地從那裏創建一個簡單的HTML窗體和POST圖像。

我也可以使用postman來獲取請求,但是當我嘗試使用我的java方法(下面)時,req.file在服務器上始終未定義。

調用的方法

public static void testPostImage() throws Exception { 
    String charset = "UTF-8"; 

    String requestURL = "http://localhost:3000/accounts/2/details"; 

    try { 
     File outputfile = new File(Util.getWorkingDirectory() + "/details"); 

     MultipartUtility multipart = new MultipartUtility(requestURL, charset); 
     multipart.addFilePart("details", new File(outputfile.getAbsolutePath())); 

     List<String> response = multipart.finish(); 
     General.println("SERVER REPLIED:"); 
     for (String line : response) { 
      General.println(line); 
     } 
    } catch (IOException ex) { 
     General.println(ex); 
    } 
} 

的MultipartUtility類來源於此tutorial

構造和 'addFilePart' 的內容...

構造

public MultipartUtility(String requestURL, String charset) 
     throws IOException { 
    this.charset = charset; 

    // creates a unique boundary based on time stamp 
    boundary = "===" + System.currentTimeMillis() + "==="; 

    URL url = new URL(requestURL); 
    httpConn = (HttpURLConnection) url.openConnection(); 
    httpConn.setUseCaches(false); 
    httpConn.setDoOutput(true); // indicates POST method 
    httpConn.setDoInput(true); 
    httpConn.setRequestProperty("Content-Type", 
      "multipart/form-data; boundary=" + boundary); 
    httpConn.setRequestProperty("User-Agent", "CodeJava Agent"); 
    httpConn.setRequestProperty("Test", "Bonjour"); 
    outputStream = httpConn.getOutputStream(); 
    writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), 
      true); 
} 

addFilePart

private static final String LINE_FEED = "\r\n"; 

public void addFilePart(String fieldName, File uploadFile) 
     throws IOException { 

    General.println("Sending to " + fieldName + ": " + uploadFile.getAbsolutePath()); 

    // Add boundary 
    writer.append("--" + boundary).append(LINE_FEED); 

    // Add form data 
    writer.append("Content-Disposition: form-data;" 
      + "name=\"myFile\";" 
      + "filename=\"" + fieldName + "\"" 
      + "\nContent-Type: text/plain\n\n").append(LINE_FEED); 

    writer.append("Content-Type: " + "multipart/form-data").append(LINE_FEED); 
    writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED); 
    writer.append(LINE_FEED); 
    writer.flush(); 

    FileInputStream inputStream = new FileInputStream(uploadFile); 
    byte[] buffer = new byte[4096]; 
    int bytesRead = -1; 
    while ((bytesRead = inputStream.read(buffer)) != -1) { 
     outputStream.write(buffer, 0, bytesRead); 
    } 
    outputStream.flush(); 
    inputStream.close(); 

    writer.append(LINE_FEED); 
    writer.flush(); 
} 

回答

0

他們可能更給它,但你不」你的右行結束於你的Content-disposition和之間標題:

// Add form data 
writer.append("Content-Disposition: form-data;" 
     + "name=\"myFile\";" 
     + "filename=\"" + fieldName + "\"").append(LINE_FEED); 

writer.append("Content-Type: text/plain").append(LINE_FEED).append(LINE_FEED); // need 2 line feeds 

而且你混合順序/類型頭

writer.append("Content-Type: " + "multipart/form-data").append(LINE_FEED); 
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED); 

的問題比你的編碼數據。

See examples here

+0

我試過了你的建議,我仍然收到同樣的錯誤。 既然它是與郵遞員和HTML形式工作,它絕對是這個Java的權利? –

+0

我會很確定它的Java。你能打印出java代碼正在發送什麼,所有的HTTP頭文件和零件表頭文件?我很確定他們是你的HTTP請求中的一些語法錯誤。 – Victory

+0

http://pastebin.com/gnnaK9ZF 我發現了一些我認爲可能是解決方案但它不正確的東西。 根據你的例子,我改變了內容的性格以符合我認爲正確的內容。 –

相關問題