Spring MVC - 将图像复制到 WEB-INF/assets 文件夹中



我正在尝试将图像复制到WEB-INF文件夹内的资产文件夹中。以下代码成功复制项目外部的图像,但无法在 WEB-INF 文件夹中复制。

public static void copyFile(String source, String destination) throws IOException {
try {
File sourceFile = new File(source);
File destinationFile = new File(destination);
FileInputStream fileInputStream = new FileInputStream(sourceFile);
FileOutputStream fileOutputStream = new FileOutputStream(destinationFile);
int bufferSize;
byte[] bufffer = new byte[512];
while ((bufferSize = fileInputStream.read(bufffer)) > 0) {
fileOutputStream.write(bufffer, 0, bufferSize);
}
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
throw new IOException(e.getMessage());
}
}

我从Http请求中获得了图像路径。

CopyFile.copyFile(imageUrl, "http://localhost:8080/M.S.-Handloom-Fabrics/static/"+imageName+".png");

我已经映射了调度程序-servlet中的资源.xml

<mvc:resources mapping="/static/**" location="/WEB-INF/assets/"/>

这是错误

信息: http://localhost:8080\M.S.-手摇织机-结构\静态\TueJun1216_27_54NPT20180.png (文件名、目录名或卷标语法不正确(

http://localhost:8080/M.S.-Handloom-Fabrics/static/"+imageName+".png"

是一个 URL,而不是文件路径,因此作为 File 构造函数的参数毫无意义。

如果您将应用程序服务器配置为在部署时分解您的 Webapp,那么您可以使用ServletContext.getRealPath但正如此 SO 帖子详细介绍的那样,您很可能不想这样做,因为您保存的文件将在重新部署时丢失。

将它们保存在 Web 应用程序之外是要走的路。

相关内容

最新更新