使用freemarker上传文件时的HTTP状态500

时间:2010-12-13 10:59:45

标签: spring spring-mvc

我正在尝试使用spring和hibernate技术在我的freemarker页面(ftl文件)中添加图片上传 - 这是我每次运行应用程序时出现的错误:

  

HTTP状态500 - 服务器   遇到内部错误()   阻止它实现这一目标   请求。

这是代码:

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英尺文件:

<input type="file" id="image" name="image" value="">

4-web 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";

}

1 个答案:

答案 0 :(得分:1)

务必按照Documentation的字母进行操作。通常,您是否正确定义了<form>元素及其enctype属性?

<form method="post" action="/form" enctype="multipart/form-data">

有关更多提示,需要日志,正如其他评论已经说过的那样。如果没有写入特定日志,请务必将记录器的阈值配置为较低级别;虽然500代码应该与ERROR级别的日志相关,但很少被过滤掉。仔细检查您的日志配置! :)

HTH