当前位置: 首页 > 知识库问答 >
问题:

Spring Boot-在远程机器上上传文件

东郭凯捷
2023-03-14

我想在远程服务器上上传文件,目前我只能在本地机器上上传。下面是我的代码

@PostMapping("/upload")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
    String fileName = fileStorageService.storeFile(file);

    String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
            .path("/downloadFile/")
            .path(fileName)
            .toUriString();

    return new UploadFileResponse(fileName, fileDownloadUri,file.getContentType(), file.getSize());
}

file.upload-dir=C:\\Test

提前感谢!

共有1个答案

毛宏达
2023-03-14

编辑:

1、用例:您想在本地上传文件(即您的应用程序正在运行的位置):

您创建了StorageService接口和一个实现类FileSystemStorageService:

@Service
public class FileSystemStorageService implements StorageService {

    private final Path rootLocation;

    @Autowired
    public FileSystemStorageService(StorageProperties properties) {
        this.rootLocation = Paths.get(properties.getLocation());
    }

    @Override
    public void store(MultipartFile file) {
        String filename = StringUtils.cleanPath(file.getOriginalFilename());
        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);
            }
            try (InputStream inputStream = file.getInputStream()) {
                Files.copy(inputStream, this.rootLocation.resolve(filename),
                    StandardCopyOption.REPLACE_EXISTING);
            }
        }
        catch (IOException e) {
            throw new StorageException("Failed to store file " + filename, e);
        }
    }

和控制器类:

@Controller
public class FileUploadController {

    private final StorageService storageService;

    @Autowired
    public FileUploadController(StorageService storageService) {
        this.storageService = storageService;
    }


    @PostMapping("/")
    public String handleFileUpload(@RequestParam("file") MultipartFile file,
            RedirectAttributes redirectAttributes) {

        storageService.store(file);
        redirectAttributes.addFlashAttribute("message",
                "You successfully uploaded " + file.getOriginalFilename() + "!");

        return "redirect:/";
    }

你可以在下面找到整个样品https://github.com/spring-guides/gs-uploading-files.

2、用例:要将文件上载到远程服务器:

在这种情况下,我建议使用SFTP。

您创建了一个实现存储服务(已在第一个用例中创建)的RemoteFileSystemStorageService。

@Service
public class RemoteFileSystemStorageService implements StorageService {

    @Autowired
    private StorageProperties properties

    final private ChannelSftp channelSftp;



     @PostConstruct
     public void setUpSsh(){
     JSch jsch = new JSch();
     Session jschSession = jsch.getSession(properties.getUsername(), 
     properties.getRemoteHost());
     jschSession.setPassword(properties.getPassword());
     jschSession.connect();
     this.channelSftp = (ChannelSftp)jschSession.openChannel("sftp");     
     }


    @Override
    public void store(MultipartFile file) {
        String filename = StringUtils.cleanPath(file.getOriginalFilename());
        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);
            }
            try (InputStream inputStream = file.getInputStream()) {
              this.channelSftp.connect();
              this.channelSftp.put(inputStream, properties.getRemoteServerDirectory());
            }
        }
        catch (IOException e) {
            throw new StorageException("Failed to store file " + filename, e);
        }
        finally{
            this.channelSftp.close();

        }
    }
 类似资料:
  • 问题内容: 我正在使用使用大量ajax的grails创建一个web应用程序。我想使用ajax实现文件上传。我不知道如何使用ajax进行文件上传。我的示例GSP代码是: 我尝试了和。上传后,我想用结果更新“ updateArea”。结果,我打算显示上传文件的详细信息。 问题答案: 通过Ajax上载文件实际上是不可能的。您仍然可以使用隐藏的iframe在后台上传文件,并评估响应(位于iframe内)或

  • 我在SeleniumWebDriver中使用Robot类创建了一个文件上传脚本。它工作正常,脚本按预期执行。现在,自动化设置在服务器上完成,并在jenkins上安排一个作业定期执行。当远程桌面连接保持打开时,脚本可以正常执行,但当连接最小化/终止时,文件上载失败。有人能帮忙吗?当远程连接最小化/终止时,robot api无法定位元素的原因。 如果您可以提供其他选项在远程服务器上执行此文件上载,这将

  • 我正在尝试找到一个解决方案,用于将文件从我的本地windows机器或本地linux机器复制到云上的hdfs。 我知道一个办法, 远程->群集节点(使用scp) 节点->hdfs(使用hdfs命令) 但是对于我的用例,它不起作用,我需要找到一个解决方案,直接上传文件从本地到hdfs,就像hue做的上传。 我也试着遵循命令 hdfs dfs-copyfromlocal file:://<'local-

  • 我只是写了一个测试,应该通过webApp下载pdf文件(是的,我知道,我不应该在selenium上做,但是你知道,订单。) 我需要什么? 对于不同的场景,我必须下载不同的pdf,重命名并放置到自定义目录。所以,我必须处理系统模态窗口。一切都很好,所以测试是在远程主机上运行的,当我点击下载文件时,我处理系统模态窗口(我使用机器人包,它是扩展的机器人类,允许我们在远程主机上使用机器人类),所以我使用机

  • 机器A和B之间的通信工作良好。我可以运行像或这样的命令,它会给出预期的结果: 我听说过,但还没有尝试过,但据我所知,这并不能解决我的问题。 有什么方法可以直接使用来实现这一点。解决办法可能是使用连接到远程主机,并直接从远程主机使用客户机,但我希望尽可能避免这种解决方案。 在上运行,而不是在本地计算机上运行。

  • 本文向大家介绍详解SpringBoot文件上传下载和多文件上传(图文),包括了详解SpringBoot文件上传下载和多文件上传(图文)的使用技巧和注意事项,需要的朋友参考一下 最近在学习SpringBoot,以下是最近学习整理的实现文件上传下载的Java代码: 1、开发环境: IDEA15+ Maven+JDK1.8 2、新建一个maven工程:   3、工程框架   4、pom.xml文件依赖项