2012-05-22 44 views
1

我試圖發送一個csv文件在發佈請求,並希望獲得服務器的csv數據。我使用球衣來創建web服務。什麼應該是發送csv時的內容類型文件應該是什麼@Consumes(__)在服務器端註釋 請幫只使用csv在休息服務使用球衣

球衣代碼

@POST 

@Consumes("text/plain") 
@Produces("text/plain") 
public String respondToPost(String incomingCsv) throws IOException { 
    return incomingCsv; 
} 

CSV文件:

email,group_email 
[email protected],[email protected] 
[email protected],[email protected] 

這將返回:

----0246824681357ACXZabcxyz 
Content-Type: application/x-www-form-urlencoded; charset=UTF-8 
Content-Transfer-Encoding: 8bit 
Content-ID: <startpart> 
Content-Disposition: form-data; name="root-fields" 


----0246824681357ACXZabcxyz 
Content-Type: application/vnd.ms-excel 
Content-Transfer-Encoding: binary 
Content-ID: <group.csv> 
Content-Disposition: attachment; name="file attachment"; filename="group.csv" 

email,group_email 
[email protected],[email protected] 
[email protected],[email protected] 

----0246824681357ACXZabcxyz-- 

,而我只想要

email,group_email 
[email protected],[email protected] 
[email protected],[email protected] 

我改變了代碼,但現在我m如果415不支持媒體type.my應用在谷歌應用程序引擎託管

@POST 
@Consumes(MediaType.MULTIPART_FORM_DATA) 
@Produces("text/plain") 
public String respondToPost(@FormDataParam("file") InputStream csv) throws IOException { 
    CSVReader csvReader = new CSVReader(new InputStreamReader(csv)); 
    List<String[]> rows = csvReader.readAll(); 
    return rows.get(1)[0]; 

} 

回答

1

您可以使用text\plain mediatype來接收請求,並使用OpenCSV來解析您的請求正文。

+0

謝謝,我試過了你的建議,但沒有得到我想要的,請看更新的問題 – vishesh

+0

好吧,你發送整個文件。不是它的內容。對於文件上傳見http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/ –

+0

嘿我只是一個初學者在這裏,我需要進一步help.Please看到更新的問題 – vishesh

相關問題