我正在尝试使用 spring 和 hibernate 技术在我的 freemarker 页面(ftl 文件)中添加图像上传 - 这是我每次运行应用程序时遇到的错误:
HTTP Status 500 - The server encountered an internal error () that prevented it from fulfilling this request.
这是代码:
1-POM 文件:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
2-app-config.xml:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
3-ftl 文件:
<input type="file" id="image" name="image" value="">
4-网络 Controller :
@RequestMapping(method= RequestMethod.POST)
public String post(Model model , HttpServletRequest req , HttpSession session,@RequestParam("image") MultipartFile multipartFile) throws IOException{
// transfer the uploaded image to the place where images exist in project
multipartFile.getBytes();
File destination = new File("/home/user/Pictures/" + multipartFile.getOriginalFilename());
multipartFile.transferTo(destination);
// delete the original uploaded image
destination.delete();
return "redirect:index";
}
最佳答案
请务必遵循Documentation至此。通常,您是否定义了 <form>
正确的元素,其 enctype
属性?
<form method="post" action="/form" enctype="multipart/form-data">
对于更多提示,需要日志,正如其他评论已经说过的那样。如果没有写入特定的日志,那么请务必将记录器的阈值配置为较低的级别;尽管 500 代码应该与 ERROR 级别的日志相关,但很少会被过滤掉。仔细检查您的日志配置! :)
HTH
关于spring - 使用 freemarker 上传文件时的 HTTP 状态 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4428115/