当我尝试通过 JAX-RS REST API 上传 csv 格式的 excel 文件时,我从浏览器收到 415(不支持的媒体类型)。Web 应用程序在嵌入式 Tomcat 服务器下部署和运行。
如果我在不是嵌入式 tomcat 的 tomcat 7 服务器中部署我的 web 应用程序,我可以成功上传 csv 文件。
感谢您为解决我的问题提供的意见。
网址: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 脚本代码:
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>