IDEA Download resource failed 解决办法

博客给出了一个信息技术相关的命令“mvn dependency:resolve -Dclassifier=sources”,该命令可能用于解决Mvn依赖问题或下载资源。

mvn dependency:resolve -Dclassifier=sources

好的,我将为您提供一个Java Web项目中实现文件压缩上传、查询和下载的详细代码示例。这个示例将使用Spring Boot框架,并结合前端HTML和JavaScript代码。 1. 首先,创建一个Spring Boot项目,并添加必要的依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> ``` 2. 创建文件实体类: ```java @Entity public class FileEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String fileName; private String filePath; private String uploadTime; // Getters and setters } ``` 3. 创建文件仓库接口: ```java public interface FileRepository extends JpaRepository<FileEntity, Long> { List<FileEntity> findByFileNameContaining(String fileName); } ``` 4. 创建文件服务类: ```java @Service public class FileService { @Autowired private FileRepository fileRepository; public void saveFile(MultipartFile file, String uploadDir) throws IOException { String fileName = StringUtils.cleanPath(file.getOriginalFilename()); String filePath = uploadDir + "/" + fileName; File dest = new File(filePath); file.transferTo(dest); FileEntity fileEntity = new FileEntity(); fileEntity.setFileName(fileName); fileEntity.setFilePath(filePath); fileEntity.setUploadTime(LocalDateTime.now().toString()); fileRepository.save(fileEntity); } public List<FileEntity> searchFiles(String keyword) { return fileRepository.findByFileNameContaining(keyword); } public Resource loadFileAsResource(String fileName) { try { Path filePath = Paths.get("uploads").resolve(fileName).normalize(); Resource resource = new UrlResource(filePath.toUri()); if(resource.exists()) { return resource; } else { throw new FileNotFoundException("File not found " + fileName); } } catch (MalformedURLException | FileNotFoundException ex) { throw new RuntimeException(ex); } } } ``` 5. 创建控制器: ```java @Controller public class FileController { @Autowired private FileService fileService; @PostMapping("/upload") public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { try { fileService.saveFile(file, "uploads"); redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!"); } catch (IOException e) { e.printStackTrace(); redirectAttributes.addFlashAttribute("message", "Upload failed: " + e.getMessage()); } return "redirect:/"; } @GetMapping("/search") public String searchFiles(@RequestParam("keyword") String keyword, Model model) { List<FileEntity> files = fileService.searchFiles(keyword); model.addAttribute("files", files); return "search"; } @GetMapping("/download/{fileName:.+}") public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) { Resource resource = fileService.loadFileAsResource(fileName); String contentType = null; try { contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath()); } catch (IOException ex) { // Ignore } if(contentType == null) { contentType = "application/octet-stream"; } return ResponseEntity.ok() .contentType(MediaType.parseMediaType(contentType)) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"") .body(resource); } } ``` 6. 创建前端页面: - upload.html ```html <!DOCTYPE html> <html> <head> <title>Upload File</title> </head> <body> <h1>Upload File</h1> <form method="POST" enctype="multipart/form-data" action="/upload"> <input type="file" name="file" /> <button type="submit">Upload</button> </form> </body> </html> ``` - search.html ```html <!DOCTYPE html> <html> <head> <title>Search Results</title> </head> <body> <h1>Search Results</h1> <form method="GET" action="/search"> <input type="text" name="keyword" /> <button type="submit">Search</button> </form> <ul> <#list files as file> <li> <a href="/download/${file.fileName}">${file.fileName}</a> (${file.uploadTime}) </li> </#list> </ul> </body> </html> ``` 这个示例实现了以下功能: 1. 上传文件: 用户可以通过网页上传文件,文件会被压缩并保存到服务器的"uploads"目录中。 2. 查询文件: 用户可以搜索文件名,系统会返回匹配的结果。 3. 下载文件: 用户可以下载搜索结果中的文件。 请注意,这个示例使用了Thymeleaf模板引擎来渲染前端页面,您可以根据需要进行调整。同时,这个示例使用了Spring Boot的内嵌服务器,您可以通过运行主类来启动应用程序。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值