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

标签 spring spring-mvc

我正在尝试使用 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/

相关文章:

sql - 映射用 Hibernate 返回表的 postgres 函数

java - spring amqp rabbitmq MessageListener 不工作

java - 将 Envers 修订添加到现有且未经审核的表

java - 无法弄清楚为什么 org.springframework.beans.factory.BeanCreationException

java - Spring Boot url 端点

java - 为什么 gradle jettyRunWar 任务不适用于 java8?

java - Spring 测试 - @ContextConfiguration 排除基类上下文

java - jsp <form :errors> 中没有消息显示

java - Java 和 Spring 中的 URL 重写

mongodb - 如何在 mongodb 中存储 byte[] 图像以用于 Doocr 进程