【发布时间】:2020-09-05 00:47:16
【问题描述】:
我目前正在我的 Spring Boot 项目中进行文件上传。我正在为我的控制器使用休息控制器,因为那是我正在使用的教程中写的 https://www.callicoder.com/spring-boot-file-upload-download-jpa-hibernate-mysql-database-example/
但是,我发现显然休息控制器无法显示基于这里写的 HTML 页面:How to return a html page from a restful controller in spring boot?
有没有办法在保留其余控制器的同时显示我的 HTML 页面?
这是我的控制器
@RestController
public class RekonsiliasiController {
private static final Logger logger = LoggerFactory.getLogger(RekonsiliasiController.class);
@Autowired
private DBFileStorageService dbFileStorageService;
@RequestMapping("/rekonsiliasi")
public String index() {
System.err.println("MASUK PAK EKO OI");
return "rekonsiliasi";
}
@PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
DBFile dbFile = dbFileStorageService.storeFile(file);
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/downloadFile/")
.path(dbFile.getId())
.toUriString();
return new UploadFileResponse(dbFile.getFileName(), fileDownloadUri,
file.getContentType(), file.getSize());
}
@GetMapping("/downloadFile/{fileId}")
public ResponseEntity<ByteArrayResource> downloadFile(@PathVariable String fileId) {
// Load file from database
DBFile dbFile = dbFileStorageService.getFile(fileId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(dbFile.getFileType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + dbFile.getFileName() + "\"")
.body(new ByteArrayResource(dbFile.getData()));
}
}
这是我的 rekonsiliasi.html
<form method="POST" action="/uploadFile" enctype="multipart/form-data">
<input type="file" name="file" /><br/><br/>
<input type="submit" value="Submit" />
</form>
这是我目前得到的,一个带有简单文本的空白页
更新
我尝试在索引和文件上传到以下之间进行划分。
RekonsiliasiController.java
@Controller
public class RekonsiliasiController {
@RequestMapping("/rekonsiliasi")
public String index() {
System.err.println("MASUK PAK EKO OI");
return "rekonsiliasi";
}
文件控制器.java
@RestController
public class FileController {
private static final Logger logger = LoggerFactory.getLogger(FileController.class);
@Autowired
private DBFileStorageService dbFileStorageService;
@PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
System.err.println("CEK");
DBFile dbFile = dbFileStorageService.storeFile(file);
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/downloadFile/")
.path(dbFile.getId())
.toUriString();
return new UploadFileResponse(dbFile.getFileName(), fileDownloadUri,
file.getContentType(), file.getSize());
}
@GetMapping("/downloadFile/{fileId}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileId) {
// Load file from database
DBFile dbFile = dbFileStorageService.getFile(fileId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(dbFile.getFileType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + dbFile.getFileName() + "\"")
.body(new ByteArrayResource(dbFile.getData()));
}
}
现在 HTML 显示得非常好,但是当我上传文件时出现 403 错误。在我试图找到这部分的问题之前,我想知道是否有一些方法可以在保留其余控制器的同时显示我的 HTML 页面?
编辑删除 FileController.Java 中的 'uploadMultipleFiles' 方法,它仍然给我 403 错误
【问题讨论】:
标签: java spring spring-boot rest