Java:使用唯一名称重命名 MultiPartFile 并将其存储到文件夹并将 url 保存到数据库

bey*_*lah 5 java spring-boot jakarta-ee

我有一个基于 Spring Boot 的后端,基于 Angular 5 的前端。通过 Angular 接收 MultiPartFile 后,我想用唯一的名称(表的 id)重命名该文件并将该文件存储到文件夹中,然后放入该文件的 URL到数据库 PostgreSQL。请帮助我,我测试了很多方法都没有任何好的结果。我尝试将 MultiPartFile 转换为文件,但我不知道该怎么做。请帮我

这是包含处理 MultiPartFile 的方法的类:

package smart.syndic.metier;

import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileSystemUtils;
import org.springframework.web.multipart.MultipartFile;

@Service
public class StorageService 
{
Logger logger = LoggerFactory.getLogger(this.getClass().getName());
private final Path rootLocation = Paths.get("fichiers/prestataires");

public void store(MultipartFile file) {
    try {

        Files.copy(file.getInputStream(), 
this.rootLocation.resolve(file.getOriginalFilename()), 
StandardCopyOption.REPLACE_EXISTING);
    } catch (Exception e) {
        throw new RuntimeException("FAIL!");
    }
 }

public Resource loadFile(String filename) {
    try {
        Path file = rootLocation.resolve(filename);
        Resource resource = new UrlResource(file.toUri());
        if (resource.exists() || resource.isReadable()) {
            return resource;
        } else {
            throw new RuntimeException("FAIL!");
        }
    } catch (MalformedURLException e) {
        throw new RuntimeException("FAIL!");
    }
 }

public void deleteAll() {
    FileSystemUtils.deleteRecursively(rootLocation.toFile());
 }
}
Run Code Online (Sandbox Code Playgroud)

这是 RestController :

package smart.syndic.web;

import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import  org.springframework.web.servlet.mvc.method.annotation.
MvcUriComponentsBuilder;
import smart.syndic.dao.PrestatairesRepository;
import smart.syndic.dao.PrestatairesTypesRepository;
import smart.syndic.entities.Prestataires;
import smart.syndic.metier.StorageService;

@RestController
@CrossOrigin("*")
public class PrestatairesRestController 
{
@Autowired
private PrestatairesRepository repository;

@Autowired
private PrestatairesTypesRepository repository2;

@Autowired
private StorageService storageService;

private List<String> files = new ArrayList<String>();

@RequestMapping(value="/prestataires/{id}", 
        method=RequestMethod.GET)
public Prestataires getVilles(@PathVariable Long id)
{
    return repository.findOne(id);
}

@RequestMapping(value="/prestataires", 
        method=RequestMethod.POST)
public Prestataires addVilles(Prestataires p,
        @RequestParam("multipartFile") MultipartFile file)
{
    try{
        storageService.store(file);
        files.add(file.getOriginalFilename());
        p.setPhoto("fichiers/prestataires/"+file.getOriginalFilename());

    }catch(Exception e){
        e.printStackTrace();
    }


 p.setPrestatairesTypes(repository2.findOne(p.getPrestatairesTypes()
 .getId()));

    return repository.save(p);              
}

}
Run Code Online (Sandbox Code Playgroud)

这是实体 Prestataires :

package smart.syndic.entities;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@Entity
public class Prestataires implements Serializable 
{
@Id @GeneratedValue
private Long id;
private String nom;
private String email;
private String tele;
private String fax;
private String rib;
private String adresse;
private String taches;
private String photo;

//-------------------Constructors--------------------
//-------------------Getters and Setters--------------------


@ManyToOne
@JoinColumn(name="ID_PRESTATAIRES_TYPES")
private PrestatairesTypes prestatairesTypes;
}
Run Code Online (Sandbox Code Playgroud)

VK3*_*321 4

如果文件名只是问题,那么您的代码没问题,您所需要做的就是使用某些字符串创建文件名,或者像下面一样清理它

//Create custom filename 
String filename = StringUtils.cleanPath(file.getOriginalFilename());
//remove spaces and make lowercase    
filename = filename.toLowerCase().replaceAll(" ", "-");
Run Code Online (Sandbox Code Playgroud)

或者

String filename = p.getId()+"."+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
Run Code Online (Sandbox Code Playgroud)

然后在你的复制方法中

Files.copy(file.getInputStream(), this.rootLocation.resolve(filename),
                StandardCopyOption.REPLACE_EXISTING); 
Run Code Online (Sandbox Code Playgroud)

这是例子

@Service
public class UploadService {

@Value("${app.image.cdn}")
private String imageCDN;

@Value("${app.upload.path}")
private String uploadPath;

private Path rootLocation;


public String store(MultipartFile file) {

    String storedLocation;
    rootLocation = Paths.get(uploadPath);

    String filename = StringUtils.cleanPath(file.getOriginalFilename());
    filename = filename.toLowerCase().replaceAll(" ", "-");

    try {
        if (file.isEmpty()) {
            throw new StorageException("Failed to store empty file " + filename);
        }
        if (filename.contains("..")) {
            // This is a security check
            throw new StorageException(
                    "Cannot store file with relative path outside current directory "
                            + filename);
        }
        Files.copy(file.getInputStream(), this.rootLocation.resolve(filename),
                StandardCopyOption.REPLACE_EXISTING);

        storedLocation = imageCDN+filename;

    }
    catch (IOException e) {
        throw new StorageException("Failed to store file " + filename, e);
    }


    return storedLocation;
}
}
Run Code Online (Sandbox Code Playgroud)

和你的 application.properties

spring.servlet.multipart.max-file-size=20MB
spring.servlet.multipart.max-request-size=20MB

app.upload.path=/uploads
app.image.cdn=http://yourhost.com/uploads/
Run Code Online (Sandbox Code Playgroud)

和你的 StorageException 类

public class StorageException extends RuntimeException {

  public StorageException(String message) {
    super(message);
  }

  public StorageException(String message, Throwable cause) {
    super(message, cause);
  }
}
Run Code Online (Sandbox Code Playgroud)