用spring boot和vue写一个批量上传excel文件到sftp的代码,其中要包含controller层,service层,serviceImpl层
时间: 2024-05-20 17:17:28 浏览: 23
Controller层代码:
@RestController
@RequestMapping("/excel")
public class ExcelController {
@Autowired
private ExcelService excelService;
@PostMapping("/upload")
public ResponseEntity<String> uploadExcel(@RequestParam("file") MultipartFile file) {
try {
excelService.uploadExcel(file);
return ResponseEntity.ok("Excel file uploaded successfully.");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error occurred while uploading Excel file.");
}
}
}
Service层代码:
public interface ExcelService {
void uploadExcel(MultipartFile file) throws Exception;
}
@Service
public class ExcelServiceImpl implements ExcelService {
@Value("${sftp.host}")
private String sftpHost;
@Value("${sftp.port}")
private int sftpPort;
@Value("${sftp.username}")
private String sftpUsername;
@Value("${sftp.password}")
private String sftpPassword;
@Value("${sftp.directory}")
private String sftpDirectory;
@Override
public void uploadExcel(MultipartFile file) throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(sftpUsername, sftpHost, sftpPort);
session.setPassword(sftpPassword);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
sftpChannel.cd(sftpDirectory);
InputStream inputStream = file.getInputStream();
sftpChannel.put(inputStream, file.getOriginalFilename());
sftpChannel.disconnect();
session.disconnect();
}
}
注意:在以上代码中,需要在application.properties文件中配置sftp相关参数,例如:
sftp.host=192.168.1.1
sftp.port=22
sftp.username=admin
sftp.password=123456
sftp.directory=/upload
另外,需要在pom.xml中添加jsch和spring-boot-starter-validation依赖:
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)