【问题标题】:Upload file using Spark framework and FreeMarker使用 Spark 框架和 FreeMarker 上传文件
【发布时间】:2017-04-14 02:55:15
【问题描述】:

我正在尝试创建一种使用 spark 框架和 freemarker 上传文件的方法,但我似乎在 .getPart 方法上遇到了障碍。我当前的 freemarker 代码如下所示:

<form method='post' enctype='multipart/form-data'>  
    <div>
        <input type='file' name='uploadedFile'>
        <button>Upload csv</button>
    </div>
</form>

我的 spark java post 方法代码如下:

post(new Route("/pdf", "multipart/form-data") {
        @Override
        public Object handle(Request request, Response response) {
            String name = null;


            File upLoadM = new File("messages/");

            Path tempFile = null;
            Part file = null;

            try {
                tempFile = Files.createTempFile(upLoadM.toPath(), "", "");
                    System.out.println(tempFile);
            } 
            catch (IOException e1) {
                e1.printStackTrace();
            }


            request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));

            try {
                file = request.raw().getPart("uploadedFile");
                System.out.println(file);
            } 
            catch (IOException | ServletException e1) {
                e1.printStackTrace();
            }

            try (InputStream input = file.getInputStream()) {
                Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING);
            } 
            catch (IOException e) {
                e.printStackTrace();
            }


            response.status(201);
            response.redirect("/pdf");
            return "";
        }
    });

当我点击上传按钮时,我收到 500 内部错误。不知道它在 .getPart 方法上崩溃的原因是什么。任何帮助将不胜感激。

【问题讨论】:

    标签: java freemarker spark-framework


    【解决方案1】:

    如果我修改代码行,结果证明它正在工作:

    request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp")); 
    

    到

    request.attribute("org.eclipse.multipartConfig", new MultipartConfigElement("/temp"));
    

    【讨论】:

      猜你喜欢
      • 2016-07-25
      • 2018-04-02
      • 2018-04-07
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 2013-10-25
      相关资源
      最近更新 更多