Java 如何在浏览器中显示上载的图像URL
我是新的多部分在春季开机,我复制了下面的代码从互联网上传文件和它的工作良好。我的要求是,在存储完文件后,我只想在浏览器中粘贴图像URL时显示它们,但使用下面的代码,在粘贴图像URL时下载它们 如何只显示上载的文件而不显示下载 控制器 删除该行:Java 如何在浏览器中显示上载的图像URL,java,spring-boot,multipartform-data,Java,Spring Boot,Multipartform Data,我是新的多部分在春季开机,我复制了下面的代码从互联网上传文件和它的工作良好。我的要求是,在存储完文件后,我只想在浏览器中粘贴图像URL时显示它们,但使用下面的代码,在粘贴图像URL时下载它们 如何只显示上载的文件而不显示下载 控制器 删除该行: .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"") 内容处置HTTP头通常触发下载行为。因此您不想使用它
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
内容处置HTTP头通常触发下载行为。因此您不想使用它。删除行.header(HttpHeaders.CONTENT\u处置,“附件;文件名=\”“+资源.getFilename()+”)
。内容处置HTTP标头通常会触发下载行为。@Codo您应该将其移动到“您的答案”amigo:)或者您也可以尝试。标头(HttpHeaders.Content_Disposition,“inline”);
@Service
public class FileStorageService {
private final Path fileStorageLocation;
@Autowired
public FileStorageService(FileStorageProperties fileStorageProperties) {
this.fileStorageLocation = Paths.get(fileStorageProperties.getUploadDir())
.toAbsolutePath().normalize();
try {
Files.createDirectories(this.fileStorageLocation);
} catch (Exception ex) {
throw new FileStorageException("Could not create the directory where the uploaded files will be stored.", ex);
}
}
public String storeFile(MultipartFile file) {
// Normalize file name
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
try {
// Check if the file's name contains invalid characters
if(fileName.contains("..")) {
throw new FileStorageException("Sorry! Filename contains invalid path sequence " + fileName);
}
// Copy file to the target location (Replacing existing file with the same name)
Path targetLocation = this.fileStorageLocation.resolve(fileName);
Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
return fileName;
} catch (IOException ex) {
throw new FileStorageException("Could not store file " + fileName + ". Please try again!", ex);
}
}
public Resource loadFileAsResource(String fileName) {
try {
Path filePath = this.fileStorageLocation.resolve(fileName).normalize();
Resource resource = new UrlResource(filePath.toUri());
if(resource.exists()) {
return resource;
} else {
throw new MyFileNotFoundException("File not found " + fileName);
}
} catch (MalformedURLException ex) {
throw new MyFileNotFoundException("File not found " + fileName, ex);
}
}
}
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")