小编Geo*_*rin的帖子

添加单个文件以gradle资源

我正在使用heroku的config var插件(请参阅https://devcenter.heroku.com/articles/config-vars).

它允许我使用.env文件,我不推送源代码控制来定义heroku环境.

因此,当我在heroku上时,我可以通过System.properties访问合理的信息.

在开发中,我想从这个文件中读取,所以如果它在我的类路径上是最好的.

.env文件是我项目的根目录,所以我不能使用这样的东西:

sourceSets {
    main {
        resources {
            srcDirs = ['src/main/resources', '/']
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

将单个文件包含到gradle资源中的最简单方法是什么?

resources gradle

5
推荐指数
3
解决办法
3686
查看次数

使用Spring Boot处理MultipartException并显示错误页面

我使用Spring Boot设置了一个非常简单的文件上传。我想知道当超过最大文件大小时是否有一种简单的方法来显示错误页面。

我已经上传了一个非常简单的示例,说明了我想在github上实现的目标。

基本上,这个想法是在全局Spring异常处理程序中捕获MultipartException:

@ControllerAdvice
public class UploadExceptionHandler {

  @ExceptionHandler(MultipartException.class)
  public ModelAndView handleError(MultipartException exception) {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("error", exception.getMessage());
    modelAndView.setViewName("uploadPage");
    return modelAndView;
  }
}
Run Code Online (Sandbox Code Playgroud)

处理文件上传的控制器非常简单:

@RequestMapping("/")
public String uploadPage() {
    return "uploadPage";
}

@RequestMapping(value = "/", method = RequestMethod.POST)
public String onUpload(@RequestParam MultipartFile file) {
    System.out.println(file.getOriginalFilename());
    return "uploadPage";
}
Run Code Online (Sandbox Code Playgroud)

还有与之关联的uploadPage.html百里香模板:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
    <title>Upload</title>
</head>
<body>

<div style="color: red" th:text="${error}" th:if="${error}">
    Error during upload
</div>

<form th:action="@{/}" method="post" enctype="multipart/form-data">
  <input type="file" id="file" …
Run Code Online (Sandbox Code Playgroud)

spring exception spring-mvc multipart spring-boot

5
推荐指数
1
解决办法
7364
查看次数