spring - Spring Boot - 在远程机器上上传文件
问题描述
我想在远程服务器上上传文件,目前我只能在本地机器上上传。下面是我的代码
@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.用例:您想在本地上传文件(即您的应用程序正在运行的地方):
您创建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
实现 StorageService(已在第一个用例中创建)。
@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();
}
}
推荐阅读
- python - 为每个 id 分区添加下月初
- python - 如何更改已被 Google 索引的 Django 网站的 URL 结构?
- sql - 添加工作班次 3 (SUM) 时的 SQL Server 疑问
- php - 使用单个请求将数据和文件提交到服务器
- r - 带有来自 Excel 数据的多条线的线图
- javascript - RXJS 显示一个时间范围内的累积结果
- c++ - 声明二维向量大小时出错
- visual-studio-code - 如何在 VSCode 的 Markdown 预览中启用复选框
- python - 用于输入信息的python程序
- hadoop - 您能否在尚不存在的表上授予对 Hive 的 Sentry 权限?