虽然我正在尝试将文件上传到球衣我正在接受以下异常,但有些人帮我解决。谢谢
Html doc: -
<div class="row form-group">
<div class="col-md-3 col-md-offset-1">
<input class="form-control" id="fileupload" type="file" name="uploadfile">
</div>
<div class="col-md-2">
<button type="button" class="form-control btn-sm btn-primary" id="file-upload">upload</button>
</div>
<div class="col-md-2">
<button type="button" class="form-control btn-sm btn-danger">cancel</button>
</div>
</div>
<br>
<div class="progress">
<div class="progress-bar progress-bar-info progress-bar-striped"
role="progressbar" aria-valuenow="20" aria-valuemin="0"
aria-valuemax="100" style="width: 20%">
<span class="sr-only">20% Complete</span>
</div>
</div>
</form>
</body>
</html>
Ajax: -
$(document).ready(function() {
$("#fileupload").click(function() {
$(this).change(function() {
var filename = $(this).val();
var ext = filename.split('.').pop().toLowerCase();
if(ext =='yaml'){
console.log(this.files[0]);
}else{
alert("."+ext+" is not vaild file extension");
$(this).val('');
}
});
});
$("#file-upload").click(function(){
var file = $('input[name="uploadfile"').get(0).files[0];
var formData = new FormData();
formData.append('uploadfile', file);
$.ajax({
url : 'http://localhost:8080/IDNS_Rule_Configuration/idns/idnsData/importYaml', //Server script to process data
type : 'POST',
data : formData,
cache : false,
contentType : 'multipart/form-data',
dataType : 'application/x-yaml',
processData : false,
success : function(data, textStatus, jqXHR) {
var userObj = JSON.parse(jqXHR.responseText);
alert(userObj);
},
error : function(jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
});
});
Java代码: -
@POST
@Consumes("multipart/form-data")
@Produces(MediaType.TEXT_PLAIN)
@Path("/importYaml")
public void uploadFile(@FormDataParam("uploadfile") InputStream uploadedInputStream,
@FormDataParam("uploadfile") FormDataContentDisposition fileDetail) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(uploadedInputStream));) {
/* int numLines = 0;*/
String line;
while( (line = reader.readLine()) != null ) {
/*numLines++;*/
System.out.println(line);
}
/* return Response.ok(Integer.toString(numLines), "text/plain").build();*/
} catch (final Exception e) {
throw new WebApplicationException(e);
}
}
例外: -
Jun 17, 2015 7:55:34 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re- throwing to the HTTP container
java.lang.NullPointerException
at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.unquoteMediaTypeParameters(MultiPartReaderClientSide.java:227)
at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readMultiPart(MultiPartReaderClientSide.java:154)
at com.sun.jersey.multipart.impl.MultiPartReaderServerSide.readMultiPart(MultiPartReaderServerSide.java:80)
at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:144)
at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:82)
at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:488)
at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:552)
at com.sun.jersey.multipart.impl.FormDataMultiPartDispatchProvider$FormDataInjectableValuesProvider.getInjectableValues(FormDataMultiPartDispatchProvider.java:122)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$VoidOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:166)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1511)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1442)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
2015年6月17日下午7:55:34 org.apache.catalina.core.StandardWrapperValve调用
请求标题: -
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/IDNS_Rule_Configuration/idns/idnsData/importYaml
Request Method:POST
Status Code:500 Internal Server Error
Request Headersview parsed
POST /IDNS_Rule_Configuration/idns/idnsData/importYaml HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 339
Accept: */*
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/39.0.2171.65 Chrome/39.0.2171.65 Safari/537.36
Content-Type: multipart/form-data
Referer: http://localhost:8080/FileUploads/index.html
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Request Payload
------WebKitFormBoundary1slIgbjdv3v2wkyD
Content-Disposition: form-data; name="uploadfile"; filename="hk.yaml"
Content-Type: application/x-yaml
------WebKitFormBoundary1slIgbjdv3v2wkyD--
Response Headersview source
Connection:close
Content-Language:en
Content-Length:3813
Content-Type:text/html;charset=utf-8
Date:Wed, 17 Jun 2015 14:34:03 GMT
Server:Apache-Coyote/1.1
答案 0 :(得分:1)
Fun3