简体   繁体   English

使用enctype =“ multipart / form-data”将选定的图像获取到控制器类吗?

[英]Get selected image to Controller Class with enctype=“multipart/form-data”?

JSP: JSP:

<form action="addUpdateSchool" method="post" enctype="multipart/form-data">
        <table align="center">
            <tr>
                <td>School Name :</td>
                <td><input type="text" id="school_name" name="school_name" /></td>
            </tr>
            <tr>
                <td>Address :</td>
                <td><textarea rows="4" cols="30" id="address" name="address"></textarea> </td>
            </tr>
             <tr>
                <td>Logo :</td>
                <td><input type="file" id="logo" name="logo"/></td>
            </tr>
             <tr>
                <td><input type="submit" onclick="return validation();" /></td>
            </tr>
        </table>
    </form>

I'm trying to get all above form properties at the Controller class including logo, but its showing null. 我正在尝试在Controller类中获取所有上述表单属性,包括徽标,但其显示为null。

How can i resolve it? 我该如何解决?

Controller Class: 控制器类别:

@RequestMapping(value = "/addSchool", method = RequestMethod.POST)
    public String addSchool(@ModelAttribute SchoolModel schoolModel, HttpSession session) {

     System.out.println("name: "+schoolModel.getSchool_name);

} 

You have to add multipartResolver in your application context in order to handle enctype="multipart/form-data" in the view page. 您必须在应用程序上下文中添加multipartResolver才能在视图页面中处理enctype="multipart/form-data" Add the following in your application contex xml file 在您的应用程序contex xml文件中添加以下内容

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

and add the appropriate jar file in your class path. 并在您的类路径中添加适当的jar文件。

For more information you can see the documentation in spring here 有关更多信息,请参见此处的春季文档。

dispatcher-servlet.xml dispatcher-servlet.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10000000" /> <!-- setting maximum upload size -->
</bean>

Controller Class: 控制器类别:

@RequestMapping(value = "/addUpdateSchool", method = RequestMethod.POST)
public String addSchool(@RequestParam("logo") MultipartFile multipartFile, @ModelAttribute SchoolModel schoolModel) {
    String path="";
    try{
        MultipartFile file = multipartFile;
        if(file.getOriginalFilename().trim().length()>0) {
            path=saveFile(file, multipartFile.getOriginalFilename());
        }else{
            path="";
        }
        System.out.println("path: "+path);
    }catch(Exception e){
        e.printStackTrace();
    }
}

public static String saveFile(MultipartFile file, String name) throws IOException{
    byte[] bytes = file.getBytes();
    Random random = new Random();       
    File dir = new File("D:\WebSLCMImages\WebSLCM\"); // Path to Save Image
    if (!dir.exists()){
        dir.mkdirs();
    }
    String path = dir+"/" + getFileName(name) + System.currentTimeMillis() + Math.abs(random.nextInt())+ getFileExtension(name);
    File serverFile = new File(path);
    BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
    stream.write(bytes);
    stream.close();
    return path;
}

Note: Don't forget to add related jar files (commons-fileupload-1.3.jar) 注意:不要忘记添加相关的jar文件(commons-fileupload-1.3.jar)

Reference link: MVC-Multipart-Resolver 参考链接: MVC-Multipart-Resolver

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当提交的表单具有属性 enctype=&quot;multipart/form-data&quot; 时,如何在控制器中获取表单数据? - How to get form data in controller when the form that was submitted has the attribute enctype="multipart/form-data"? 当表单具有enctype =“ multipart / form-data时,无法在数据库中插入图像 - Not able to Insert image in database when form has enctype="multipart/form-data enctype multipart/form-data 在多数据类型表单中的使用 - Usage of enctype multipart/form-data in multi data type form 使用enctype = multipart / form-data时,无法获取表单字段的值:java servlet - Unable to get the value of form fields when using enctype=multipart/form-data : java servlets h:form enctype =“ multipart / form-data”不执行任何操作 - h:form enctype=“multipart/form-data” not firing any action enctype =“ multipart / form-data”破坏了我的应用 - enctype=“multipart/form-data” destroyed my app 了解Spring MVC中的enctype = multipart / form-data的工作 - Understanding working of enctype = multipart/form-data in spring mvc 在 enctype=&quot;multipart/form-data&quot; 请求不起作用之后 - after enctype="multipart/form-data" request not working 使用 enctype=multipart/form-data 上传文件时出错,错误是上传的文件不是 multipart - error while file uploading using enctype=multipart/form-data , the error is the file bring uploaded is not multipart 尽管我写了“enctype=&quot;multipart/form-data”,但发生错误“当前请求不是多部分请求” - Although I wrote 'enctype="multipart/form-data', ERROR 'Current request is not a multipart request' happen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM