java - Spark (JAVA): how to read and save data file from the http body(Audio file)?

标签 java webserver spark-java

我有点知道如何查看请求中的数据,但我不知道如何处理它。

post("/", (req, res) -> {
        System.out.println(req.body());
        return "something";
    });

这向我展示了:

--1Wbh7zeSxsgY0YXI6wHO8nmxeVk4iV
Content-Disposition: form-data; name="file"; filename="1455896241350.m4a"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

Data data ... etc

如何获取正文附带的文件并将其保存在服务器中以便稍后我可以使用它?

*------------------------ * ------------------------ * ------------------------*

我已经厌倦了这段代码:

try {
        FileOutputStream fis = new FileOutputStream(new File("audio.m4a"));
        System.out.println("file created");
        try {
            int offset = 186;
            fis.write(req.bodyAsBytes(), offset, req.bodyAsBytes().length - offset);
            fis.close();
            System.out.println("file wrote");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("done");

它对我来说工作得很好,但它是静态的或硬编码的,我需要它动态。

(注意/我跳过了前 186 个字节,它们是 http 的 header ,所以我需要动态到达那里,而不是硬编码的方式)

最佳答案

get("/upload", (request, response) -> {
    return new ModelAndView(null, "upload.ftl");
}, new FreeMarkerEngine());

post("/upload", "multipart/form-data", ((request, response) -> {
    try {
        request.raw().setAttribute("org.eclipse.jetty.multipartConfig", 
            new MultipartConfigElement("/tmp", 100000000, 100000000, 1024));

        String filename = request.raw().getPart("file").getSubmittedFileName();

        Part uploadedFile = request.raw().getPart("file");
        try (final InputStream in = uploadedFile.getInputStream()) {
            Files.copy(in, Paths.get("/tmp/"+filename));
        }
        uploadedFile.delete();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return new ModelAndView(null, "upload.ftl");
}), new FreeMarkerEngine());

上传.ftl

<form action="/upload" enctype="multipart/form-data" method="post">
    <label for="file">File to send</label>
    <input type="file" name="file"><br>
    <input type="submit" value="Upload">
</form>

请原谅黑客异常处理

这需要 Spark 2.3

<dependency>
    <groupId>com.sparkjava</groupId>
    <artifactId>spark-core</artifactId>
    <version>2.3</version>
</dependency>

关于java - Spark (JAVA): how to read and save data file from the http body(Audio file)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35509355/

相关文章:

java - 方便地创建粗体版本的字体

java - 如何有效地在 SWT/Swing 表中填充数百万条记录

java - 什么是用于检测 Java 代码中的 for 循环和 while 循环的正则表达式

tcp - 二进制文件的快速传输

apache - 如何使用 Linux 对 Web 服务器进行基准测试/负载测试

java - 通过 OLSMultipleLinearRegression 计算估计值

ruby-on-rails - 在 vps 托管上配置 Rails 服务器

javascript - 在 JavaScript 中读取 TIFF 文件的响应

java - 如何修改 pom.xml 以便 Maven 3.0.4 不认为我正在运行 SDK 1.3

java - `name` 作为 freemarker 中断中的变量名