有一个控制器,我在其中@GetMapping
处理页面
orders/{id}/upload
实现以下功能的地方。
显示文件列表,其中包含目录中这些文件的链接
src/main/resources/static/layout/{id}/
并实现了一个多部分表单,通过该表单将文件上传到服务器到上面RestController
via中指示的文件夹@PostMapping
。
问题是:
当我将文件上传到服务器时,它会进入文件列表,但是如果我点击链接,我会收到 404 错误,尽管文件在服务器上。
我重新启动服务器并且链接开始工作(链接地址没有改变)。
告诉我可能是什么问题?
@Controller
public class OrderController {
@GetMapping("/orders/{id}/upload")
public String upload(@PathVariable(value = "id") long id, Model model) {
model.addAttribute("order_number", id);
String directory = "src/main/resources/static/layout/" + id + "/";
File file = new File(directory);
if (!file.exists()) {
file.mkdirs();
}
File[] listFiles = file.listFiles();
model.addAttribute("listfile", listFiles);
return "upload";
}
}
.
@Service
public class FileUploadService {
public void uploadFile(MultipartFile file, long id) throws IOException {
File uploadFile = new File("/Users/prisukhin/Documents/printing/src/main/resources/static/layout/" + id + "/" + file.getOriginalFilename());
if(file != null) {
file.transferTo(uploadFile);
}
}
}
.
@RestController
public class FileUploadRestController {
@Autowired
FileUploadService fileUploadService;
@PostMapping("/orders/{id}/upload")
public RedirectView fileUpload(@PathVariable(value = "id") long id, @RequestParam MultipartFile file_form, Model model) throws IOException {
fileUploadService.uploadFile(file_form, id);
return new RedirectView("/orders/{id}/upload");
}
upload.html 文件的一部分
<table class="table table-hover">
<tbody th:each="l : ${listfile}">
<tr>
<td>
<a th:text="${#strings.substringAfter(l, '/layout/'+order_number+'/')}" th:href="@{'../../'}+${#strings.substringAfter(l, '/static/')}" target="_blank"></a>.
</td>
</tr>
</tbody>
</table>
<form th:attr="action=@{'/orders/'} + ${order_number} + @{'/upload'}" method="POST" enctype="multipart/form-data" class="upload-form mx-auto">
<div class="input-group mb-3">
<input type="file" class="form-control" name="file_form" />
<div class="input-group-append">
<button class="btn btn-secondary" type="submit">Загрузить</button>
</div>
</div>
</form>
白标错误页面
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Sep 23 11:50:06 MSK 2022
There was an unexpected error (type=Not Found, status=404).
No message available
调试信息
2022-09-23 11:50:02.881 DEBUG 58415 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : POST "/orders/45/upload", parameters={multipart}
2022-09-23 11:50:02.947 DEBUG 58415 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.prisukhin.integral.controllers.FileUploadRestController#fileUpload(long, MultipartFile, Model)
2022-09-23 11:50:03.024 DEBUG 58415 --- [nio-8080-exec-1] o.s.web.servlet.view.RedirectView : View [RedirectView], model {}
2022-09-23 11:50:03.038 DEBUG 58415 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 302 FOUND
2022-09-23 11:50:03.046 DEBUG 58415 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : GET "/orders/45/upload", parameters={}
2022-09-23 11:50:03.047 DEBUG 58415 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.prisukhin.integral.controllers.OrderController#upload(long, Model)
2022-09-23 11:50:03.065 DEBUG 58415 --- [nio-8080-exec-2] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
2022-09-23 11:50:03.668 DEBUG 58415 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : GET "/css/style.css", parameters={}
2022-09-23 11:50:03.672 DEBUG 58415 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : GET "/img/logo-1.png", parameters={}
2022-09-23 11:50:03.675 DEBUG 58415 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2022-09-23 11:50:03.675 DEBUG 58415 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2022-09-23 11:50:03.691 DEBUG 58415 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2022-09-23 11:50:03.693 DEBUG 58415 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2022-09-23 11:50:03.728 DEBUG 58415 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2022-09-23 11:50:06.162 DEBUG 58415 --- [nio-8080-exec-5] **o.s.web.servlet.DispatcherServlet : GET "/layout/45/0000005z.xml", parameters={}
2022-09-23 11:50:06.163 DEBUG 58415 --- [nio-8080-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2022-09-23 11:50:06.164 DEBUG 58415 --- [nio-8080-exec-5] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2022-09-23 11:50:06.165 DEBUG 58415 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2022-09-23 11:50:06.173 DEBUG 58415 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2022-09-23 11:50:06.177 DEBUG 58415 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2022-09-23 11:50:06.226 DEBUG 58415 --- [nio-8080-exec-5] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2022-09-23 11:50:06.229 DEBUG 58415 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404**
该文件夹
/src/main/resources/
不用于上传用户文件。源中的此文件夹用于应用程序资源。
组装完成后,这个文件夹的所有内容都转移到
target/classes
以及已编译的类和导入
的依赖项(如果项目是由 maven 构建的)
build/resources
(如果项目是由 gradle 构建的)
编译的类被移动到
build/classes
从哪里(
target
/build
)启动应用程序。这就是为什么在重新启动之前您什么都看不到的原因。
当您发出请求时,
/layout/45/0000005z.xml
您的请求ResourceHttpRequestHandler
不是沿着路径/Users/prisukhin/Documents/printing/src/main/resources/static/layout/45/0000005z.xml
(如您所料)而是沿着路径找到它/Users/prisukhin/Documents/printing/target/static/layout/45/0000005z.xml
(如果项目是由 maven 构建的)
/Users/prisukhin/Documents/printing/build/resources/static/layout/45/0000005z.xml
(如果项目是 gradle 构建的)
我求你了,不要下载任何东西。
target/classes/
build/resources/
它们也不适用于加载用户文件。
这些是用于构建项目的临时文件夹。
如何进行?
您需要在源文件夹和项目构建文件夹之外创建一个单独的文件夹。
如果您希望它在附近,您可以创建如下内容:
/Users/prisukhin/Documents/printing/storage/
并调整静力学的回归就可以了。
为此,请将以下几行添加到方法中的 MVC 配置文件(配置类实现
WebMvcConfigurer
)中:addResourceHandlers(ResourceHandlerRegistry registry)
然后你需要修复控制器
服务
并修复链接...
他们需要参考
/storage/layout/45/0000005z.xml
配置
最好将此路径放入 application.properties 通过添加如下行:
之后,您可以在服务(和任何地方)中实现它,如下所示
在役:
在控制器中:
然后,您可以轻松地移动此应用程序,而不必担心您的应用程序被固定在您的个人计算机上。您始终可以为所需的环境指定所需的路径,它可以在您的计算机和某处的服务器上同时工作
/var/www/