日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了Java Spring-無(wú)法將圖像保存到靜態(tài)文件夾的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我想將圖像保存到resources/static/photos文件,但Java/Kotlin找不到它。但它發(fā)現(xiàn)project/photos很好。

這是用科特林編寫的代碼,但我認(rèn)為這無(wú)關(guān)緊要

    override fun saveImage(imageFile: MultipartFile, id: String) {
        val bytes = imageFile.bytes

        val path = Paths.get(
            "$imagesFolderPath$id.${imageFile.originalFilename.substringAfter('.')}")
        Files.write(path, bytes)
    }

我需要將此文件保存到resources/static/photos,以便能夠從胸腺葉訪問(wèn)它。

謝謝。

推薦答案

問(wèn)題是,您可能能夠在開發(fā)階段將文件保存在項(xiàng)目目錄中,但一旦將項(xiàng)目導(dǎo)出為應(yīng)用程序包(<[2-3]-應(yīng)用程序、.war-存檔等),就不可能做到這一點(diǎn),因?yàn)樵谶@一點(diǎn)上,以前是文件系統(tǒng)上的實(shí)際目錄的所有內(nèi)容現(xiàn)在都是單個(gè)文件。

以下是如何通過(guò)將圖像保存在可配置文件夾中來(lái)實(shí)現(xiàn)此功能的示例:

我從未用Kotlin編寫過(guò)一行代碼。我希望這個(gè)示例能夠幫助您,即使它是用Java編寫的。

這是一個(gè)示例控制器,它接受要在POST終結(jié)點(diǎn)上載并在GET終結(jié)點(diǎn)下載的圖像:

package example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Optional;

@RestController
public class MyController {

    private final Path imageStorageDir;

    /*
    The target path can be configured in the application.properties / application.yml or using the parameter -Dimage-storage.dir=/some/path/
     */
    @Autowired
    public MyController(@Value("${image-storage-dir}") Path imageStorageDir) {
        this.imageStorageDir = imageStorageDir;
    }

    @PostConstruct
    public void ensureDirectoryExists() throws IOException {
        if (!Files.exists(this.imageStorageDir)) {
            Files.createDirectories(this.imageStorageDir);
        }
    }

    /*
    This enables you to perform POST requests against the "/image/YourID" path
    It returns the name this image can be referenced on later
     */
    @PostMapping(value = "/image/{id}", produces = MediaType.TEXT_PLAIN_VALUE)
    public String uploadImage(@RequestBody MultipartFile imageFile, @PathVariable("id") String id) throws IOException {
        final String fileExtension = Optional.ofNullable(imageFile.getOriginalFilename())
                .flatMap(MyController::getFileExtension)
                .orElse("");

        final String targetFileName = id + "." + fileExtension;
        final Path targetPath = this.imageStorageDir.resolve(targetFileName);

        try (InputStream in = imageFile.getInputStream()) {
            try (OutputStream out = Files.newOutputStream(targetPath, StandardOpenOption.CREATE)) {
                in.transferTo(out);
            }
        }

        return targetFileName;
    }

    /*
    This enables you to download previously uploaded images
     */
    @GetMapping("/image/{fileName}")
    public ResponseEntity<Resource> downloadImage(@PathVariable("fileName") String fileName) {
        final Path targetPath = this.imageStorageDir.resolve(fileName);
        if (!Files.exists(targetPath)) {
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(new PathResource(targetPath));
    }

    private static Optional<String> getFileExtension(String fileName) {
        final int indexOfLastDot = fileName.lastIndexOf('.');

        if (indexOfLastDot == -1) {
            return Optional.empty();
        } else {
            return Optional.of(fileName.substring(indexOfLastDot + 1));
        }
    }
}

假設(shè)您上傳了文件結(jié)尾為.png、id為HelloWorld的am圖片,然后可以使用url訪問(wèn)該圖片:
http://localhost:8080/image/HelloWorld.png

使用此URL,您還可以在任何胸腺葉模板中引用該圖像:

<img th:src="@{/image/HelloWorld.png}"></img>

這篇關(guān)于Java Spring-無(wú)法將圖像保存到靜態(tài)文件夾的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:Java Spring 保存 圖像 文件夾 靜態(tài)
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定