【问题标题】:SparkJava: Upload file did't work in Spark java frameworkSparkJava:上传文件在 Spark java 框架中不起作用
【发布时间】:2016-01-12 14:57:34
【问题描述】:

我从 stackoverflow 中获得了一些关于在 spark java 中上传文件的方法,但我尝试并没有成功。

post("/upload",
          (request, response) -> {

            if (request.raw().getAttribute("org.eclipse.jetty.multipartConfig") == null) {
                MultipartConfigElement multipartConfigElement = new MultipartConfigElement(System.getProperty("java.io.tmpdir"));
                request.raw().setAttribute("org.eclipse.jetty.multipartConfig", multipartConfigElement);
            }
            Part file = request.raw().getPart("file");
            Part name = request.raw().getPart("name");
            String filename = file.getName();
            if(name.getSize() > 0){
                try{
                    filename = IOUtils.toString(name.getInputStream(), StandardCharsets.UTF_8);
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
            Path filePath = Paths.get(".",filename);
            Files.copy(file.getInputStream(),filePath);
            return "Done!";
          });

}

我使用邮递员发送消息

我得到了这样的错误

错误指向代码Part file = request.raw().getPart("file");

【问题讨论】:

    标签: java


    【解决方案1】:
    post("/upload", "multipart/form-data", (request, response) -> {
    
    String location = "image";          // the directory location where files will be stored
    long maxFileSize = 100000000;       // the maximum size allowed for uploaded files
    long maxRequestSize = 100000000;    // the maximum size allowed for multipart/form-data requests
    int fileSizeThreshold = 1024;       // the size threshold after which files will be written to disk
    
    MultipartConfigElement multipartConfigElement = new MultipartConfigElement(
         location, maxFileSize, maxRequestSize, fileSizeThreshold);
     request.raw().setAttribute("org.eclipse.jetty.multipartConfig",
         multipartConfigElement);
    
    Collection<Part> parts = request.raw().getParts();
    for (Part part : parts) {
       System.out.println("Name: " + part.getName());
       System.out.println("Size: " + part.getSize());
       System.out.println("Filename: " + part.getSubmittedFileName());
    }
    
    String fName = request.raw().getPart("file").getSubmittedFileName();
    System.out.println("Title: " + request.raw().getParameter("title"));
    System.out.println("File: " + fName);
    
    Part uploadedFile = request.raw().getPart("file");
    Path out = Paths.get("image/" + fName);
    try (final InputStream in = uploadedFile.getInputStream()) {
       Files.copy(in, out);
       uploadedFile.delete();
    }
    // cleanup
    multipartConfigElement = null;
    parts = null;
    uploadedFile = null;
    
    return "OK";
    });
    

    这会很好用,我在https://groups.google.com/forum/#!msg/sparkjava/fjO64BP1UQw/CsxdNVz7qrAJ找到了它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2017-04-14
      • 2017-06-30
      • 2018-04-02
      • 2018-09-27
      • 2016-08-16
      • 2017-03-02
      相关资源
      最近更新 更多