Spring将上载的文件另存为公共资源
在文件系统中存储公共映像的一般方法是什么? 我在服务器上有一个Spring将上载的文件另存为公共资源,spring,server,Spring,Server,在文件系统中存储公共映像的一般方法是什么? 我在服务器上有一个端点,用于保存文件。但我不明白的是,如何使它可以公开访问,这样我就不必为这些公共文件创建端点(我认为这将是不必要的开销)?我需要添加一些单独的文件服务器吗? 我的web应用程序架构是: Angular.js client | Node.js client server | Spring backend on separate server 我想要达到的是 <img src="localhost:8000/p
端点
,用于保存文件。但我不明白的是,如何使它可以公开访问,这样我就不必为这些公共文件创建端点(我认为这将是不必要的开销)?我需要添加一些单独的文件服务器吗?
我的web应用程序架构是:
Angular.js client
|
Node.js client server
|
Spring backend on separate server
我想要达到的是
<img src="localhost:8000/public/images/myImage"/>
我是来回答的,但让endpoint真的是正确的方法吗?您可以将图像保存到任何路径,但不必保存到webroot。您可以在显示图像时从该目录中读取图像 ImageController.java
package com.expertwebindia;
import java.io.File;
import java.io.FileInputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ImageController {
private String path = "E:/uploads/";
@RequestMapping(value = "/image/display",method=RequestMethod.GET)
@ResponseBody
public byte[] helloWorld(@RequestParam String name,HttpServletResponse response) {
System.out.println("Show is invoked");
response.setContentType("image/jpeg");
File file;
byte arr[]={};
try{
file = new File(path+name);
if(file.isFile()){
System.out.println("File is found");
}
else{
name = "no-image.jpg";
file = new File(path+name);
}
arr= new byte[(int)file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(arr,0,arr.length);
}catch(Exception e){
System.out.println(e);
}
return arr;
}
}
在JSP中需要显示的地方需要放置“/>
如果要在应用程序资源文件夹中存储图像,请尝试以下操作: A.资源映射(可访问此文件夹中的所有目录): D.保存图像的方法:
<http pattern="/resources/**" security="none"/>
PS:这不是一个理想的解决方案,但用于学习目的。我也在寻找免费的图像存储。我遇到了Amazon S3,但它需要信用卡,然后是Box,Dropbox。但我发现这很有趣
PPS:你可以看到我的演示应用
<mvc:resources location="/resources/" mapping="/resources/**"/>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<img src="<spring:url value='/resources/profile-pictures/${user.profileImage}'/>" class="img-circle" alt="User Image">
<http pattern="/resources/**" security="none"/>
@RequestMapping(value="/upload", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("imageFile") MultipartFile file, Principal principal) {
String webappRoot = servletContext.getRealPath("/");
String LoggedInUser = principal.getName();
User user = userService.findLoggedInUser(LoggedInUser);
//System.out.println(user.toString());
try {
if (!file.isEmpty()) {
BufferedImage src = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
File destination = new File(webappRoot + "/resources/profile-pictures/ProfileImage"+user.getName()+".jpg"); // something like C:/Users/tom/Documents/nameBasedOnSomeId.png
ImageIO.write(src, "png", destination);
System.out.println("users profile Pic is stored at "+user.getProfileImage() + "UserPassword :" +user.getPassword());
user.setProfileImage("ProfileImage"+user.getName()+".jpg");
userService.update(user);
System.out.println("Image is stored at "+ user.getProfileImage());
}
} catch (Exception e) {
System.out.println("Exception occured" + e.getMessage());
return "redirect:/account/upload?success=false";
}
return "redirect:/account/upload?success=true";
}