JAX-RS REST API を介して csv 形式の Excel ファイルをアップロードしようとすると、ブラウザーから 415 (Unsupported Media Type) が返されます。Web アプリケーションがデプロイされ、組み込みの Tomcat サーバーで実行されました。
組み込みのTomcatではないTomcat 7サーバーにWebアプリケーションをデプロイすると、csvファイルを正常にアップロードできます。
私の問題を解決するためにあなたの意見に感謝します。
URL: ipaddress:8090/config/configutil/upload
失敗したブラウザ ヘッダー リクエスト:
Request URL:http://localhost:8090/config/configutil/upload
Request Method:POST
Status Code:415 Unsupported Media Type
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:2346
Content-Type:multipart/form-data; boundary=----WebKitFormBoundarycWT1QSyGGxijtUUP
Host:localhost:8090
Origin:http://localhost:8090
Referer:http://localhost:8090/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
Request Payload
------WebKitFormBoundarycWT1QSyGGxijtUUP
Content-Disposition: form-data; name="UserExportFormat"
XML
------WebKitFormBoundarycWT1QSyGGxijtUUP
Content-Disposition: form-data; name="uploadedFile"; filename="1-06092012 (3).csv"
Content-Type: application/vnd.ms-excel
------WebKitFormBoundarycWT1QSyGGxijtUUP--
Response Headersview source
Cache-Control:no-cache
Content-Length:1117
Content-Type:text/html;charset=utf-8
Date:Tue, 18 Sep 2012 00:09:35 GMT
Server:Apache-Coyote/1.1
JAX-RS API
/**
* API will import the ConfigSet from CSV file request and returns the response in Text/Html format.
* @param is
* @param request
* @param response
*/
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_HTML)
public void upload(@FormDataParam("uploadedFile") InputStream is, @Context HttpServletRequest request, @Context HttpServletResponse response) {
// read it with BufferedReader
ObjectMapper mapper = new ObjectMapper();
PrintWriter out;
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(is));
ConfigSetResponse configSetResponse = new CSVReader().csvReader(br);
out = response.getWriter();
mapper.writeValue(out, configSetResponse);
} catch (Exception e) {
logger.error("Error while uploading :", e);
e.printStackTrace();
} finally {
// Close the file once all data has been read.
try {
if (null != br) {
br.close();
}
if (null != is) {
is.close();
}
} catch (IOException e) {
logger.error("Error while uploading :", e);
e.printStackTrace();
}
}
}
この JAX-RS API を呼び出す Java Script コード:
var importForm = $( modal ).find("#icwExportForm");
importForm.ajaxForm({
contentType: false,
success : function(responseObj) {
try{
refreshConfigSetUI(eval(responseObj));
ccw.modal.hide(modal);
}catch(e){
ccw.modal.hide(modal);
$('#runtimeErrorMessages').html(commonErrorData);
}
},
error : function(request, msg, err) {
ccw.modal.hide(modal);
$('#runtimeErrorMessages').html(commonErrorData);
}
});
importForm.attr("action", "config/configutil/upload");
importForm.attr("Content-Type" ,"multipart/form-data");
importForm.submit();
});
HTML ページ:
<form class="icwExportForm" id="icwExportForm" method="post" action="" name="ExportOptionInputForm" enctype="multipart/form-data" accept-charset="utf-8">
<div class="icwImportContent" style="padding: 5px 0px 10px 20px">
<input type="file" name="uploadedFile" value="" id="icwUploadFile" class="icwButton" size="45" tabindex="1" />
</div>
</form>