Springboot实现PDF图片上传到服务器并保存路径到数据库完整代码
时间: 2023-05-11 10:06:42 浏览: 447
非常感谢您的提问!以下是Springboot实现PDF图片上传到服务器并保存路径到数据库的完整代码:
@Controller
public class FileUploadController {
@Autowired
private FileUploadService fileUploadService;
@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
String fileType = file.getContentType();
String fileExtension = FilenameUtils.getExtension(fileName);
if (fileType.equals("application/pdf") && fileExtension.equals("pdf")) {
try {
String filePath = fileUploadService.saveFile(file);
model.addAttribute("message", "File uploaded successfully!");
model.addAttribute("filePath", filePath);
} catch (IOException e) {
model.addAttribute("message", "File upload failed: " + e.getMessage());
}
} else {
model.addAttribute("message", "Please upload a PDF file!");
}
return "uploadStatus";
}
}
@Service
public class FileUploadService {
@Value("${file.upload-dir}")
private String uploadDir;
@Autowired
private FileRepository fileRepository;
public String saveFile(MultipartFile file) throws IOException {
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
String filePath = uploadDir + "/" + fileName;
File newFile = new File(filePath);
newFile.getParentFile().mkdirs();
file.transferTo(newFile);
FileEntity fileEntity = new FileEntity();
fileEntity.setFileName(fileName);
fileEntity.setFilePath(filePath);
fileRepository.save(fileEntity);
return filePath;
}
}
@Entity
@Table(name = "files")
public class FileEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "file_name")
private String fileName;
@Column(name = "file_path")
private String filePath;
// getters and setters
}
在这个代码中,我们使用了Springboot的MultipartFile来处理上传的文件,然后通过FileUploadService将文件保存到服务器上,并将文件路径保存到数据库中。同时,我们也对上传的文件进行了一些限制,只允许上传PDF文件。如果上传的文件不符合要求,我们会返回一个错误信息。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)