如何更改保存文件目录?

发布于 2025-01-15 03:56:54 字数 693 浏览 0 评论 0原文

我想更改保存文件目录。 这是我的代码:

@RequestMapping(value = "/UploadFile")
public String uploadFile(HttpServletResponse response String base64, String name, String size) throws Exception {
    byte[] decodedFile = Base64.getDecoder().decode(base64.getBytes(StandardCharsets.UTF_8));
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=" + name);
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");

    InputStream is = new ByteArrayInputStream(decodedFile);
    IOUtils.copy(is, response.getOutputStream());
    response.flushBuffer();
    return "true";
}

I want to change saving file directory.
Here is it my code:

@RequestMapping(value = "/UploadFile")
public String uploadFile(HttpServletResponse response String base64, String name, String size) throws Exception {
    byte[] decodedFile = Base64.getDecoder().decode(base64.getBytes(StandardCharsets.UTF_8));
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=" + name);
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");

    InputStream is = new ByteArrayInputStream(decodedFile);
    IOUtils.copy(is, response.getOutputStream());
    response.flushBuffer();
    return "true";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫竹語嫣☆ 2025-01-22 03:56:54

试试这个:

 File path = new File("YOUR PATH HERE") ;
            try (FileOutputStream fos = new FileOutputStream(path)) {
                fos.write(decodedFile);
            } catch (IOException e) {
                e.printStackTrace();
            }

您还应该考虑阅读这些链接:

https://medium.com/javarevisited/how-to-upload-files-to-local-directory-in-spring-boot-c8c33f8239d3

https://spring.io/guides/gs/uploading-files/

编辑

如果字符串名称是原始文件名,您也可以这样做:

File path = new File("C:\\YOUR_PATH\\images\\" + name);

Try this:

 File path = new File("YOUR PATH HERE") ;
            try (FileOutputStream fos = new FileOutputStream(path)) {
                fos.write(decodedFile);
            } catch (IOException e) {
                e.printStackTrace();
            }

You should also consider reading these links:

https://medium.com/javarevisited/how-to-upload-files-to-local-directory-in-spring-boot-c8c33f8239d3

https://spring.io/guides/gs/uploading-files/

EDIT

If String name is original File name, you could also do this:

File path = new File("C:\\YOUR_PATH\\images\\" + name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文