FileUpload.java 文件源码

java
阅读 41 收藏 0 点赞 0 评论 0

项目:Switcharoo 作者:
@Override
public void register(Gson gson) {
    uploadDir.mkdir();

    post("/image", (req, res) -> {
        req.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
        Part part = req.raw().getPart("image");
        String[] filename = getFilenameParts(part.getSubmittedFileName());
        Path tempFile = Files.createTempFile(uploadDir.toPath(), filename[0], "." + filename[1]);

        try (InputStream input = part.getInputStream()) {
            Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING);
        } catch (Exception e) {
            LOG.error("Error while uploading file", e);
            res.status(500);
            return "";
        }

        LOG.info("File uploaded: {}", tempFile.toString());
        res.type("application/json");
        return String.format("{\"location\":\"/%s\", \"filetype\":\"%s\"}", tempFile.toString(), getFiletype(part.getContentType()));
    });
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号