当前位置: 首页 > 知识库问答 >
问题:

无法上传文件到泽西

唐弘和
2023-03-14

当我试图上传文件到泽西岛时,我发现下面有一些异常,请帮助我解决。谢谢

html文档:-

    <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>

阿贾克斯:-

$(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组织。阿帕奇。卡塔琳娜。果心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

共有1个答案

穆文斌
2023-03-14
try this may work for you

$.ajax({
                    type: "POST",
                    url: 'http://localhost:8080/IDNS_Rule_Configuration/idns/idnsData/importYaml',
                    enctype: "multipart/form-data",
                    data: formData,
                    processData: false,  
                    contentType: false,
                    success: function (data) {
    //some success functions
    }
    });
 类似资料:
  • 我正在创建一个API上传文件使用泽西。 } } 当我执行curl-v-X POST时——form file=@rt.txt url 我哪里做错了?

  • 我无法上传新泽西州的文件。使用泽西1.9。InputStream到达服务时为空。我尝试过将其作为普通表单提交和使用jQuery表单插件的AJAX提交,但结果都是一样的。也没有记录任何异常,这让它更加令人沮丧。 HTML表单 聚甲醛 Eclipse中的依赖层次结构显示mimepull.jar1.6也被引入。 剩下的服务 如果我添加@FormDataParam(“fileupload_name”)Fo

  • 我在Jersey写了一个Rest服务来上传多个文件。如下图所示。但是我需要Restservice类中的属性名,即name=“metadata”和name=“file”。 选择XML文件1: 选择PDF文件2: FormDataContentDisposition仅从表单中提取内容类型、文件名,而不是输入类型名称=“属性。 任何帮助都将不胜感激。 我张贴使用超文本标记语言的请求如下所示。 也发布HT

  • 我使用这个网络服务上传一个文件使用泽西 //省省吧 //将上传的文件保存到新位置 当我执行我的代码我得到一个405错误!对这个问题有什么建议吗?

  • 我运行一个简单的后端应用程序,允许上传文件。我使用泽西岛并在码头运行它。我的代码部分如下所示: 上传工作正常,但我发现该方法是在整个流上传到后端时调用的。因此,例如,如果我发送大文件(要上传3Gigs),我的POST请求会立即出现在后端,但只有当整个3Gigs通过网络上传时才会调用上面的方法。 我想在方法中进行一些检查,并且在某些情况下不上传文件,因此它不需要将整个内容传递到后端,然后将错误消息发

  • 我试图在AWSS3中上传错误文件,但它显示错误,如“您试图访问的存储桶必须使用指定的endpoint寻址。请将所有未来的请求发送到此endpoint:”test9011960909。s3。亚马逊。com“ 我还指定了“区域”= 当我指定时,它正在工作 但我想把文件上传到主bucket的子文件夹中 我已经应用了AWS S3批准的答案:您试图访问的桶必须使用指定的终结点进行寻址 但仍然得到同样的错误,