zoukankan      html  css  js  c++  java
  • Spring中servletFileUpload完成上传文件以及文本的处理

    JSP:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
                <form action="test.do" method="post" enctype="multipart/form-data">     
                                                        名字:<input name="username" type="text"><br />
                    <input name="imgFile" id="imgFile" type="file" /><br />  
                    <input type="submit" value="提交">
                </form> 
    </body>
    </html>
    

      JAVA:

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.FileUploadException;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    import org.springframework.data.mongodb.core.query.Criteria;
    import org.springframework.data.mongodb.core.query.CriteriaDefinition;
    import org.springframework.data.mongodb.core.query.Query;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    @Controller
    public class tt
    {
        @RequestMapping(value = "test")
        public void test(HttpServletRequest request)
        {
            boolean flag = ServletFileUpload.isMultipartContent(request);
    
            if (flag)
            {
                DiskFileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                try
                {
                    // 解析request请求
                    List<FileItem> list = upload.parseRequest(request);
                    for (FileItem item : list)
                    {
                        System.out.println(item);
    
                        String fileName = item.getFieldName();
                        // 判断是否是普通的输入项
                        if (item.isFormField())
                        {
                            // 解析文本
                            if (fileName.equals("username"))
                            {
                                try
                                {
                                    String username = item.getString("UTF-8");
                                    System.out.println(username);
                                }
                                catch (UnsupportedEncodingException e)
                                {
                                    e.printStackTrace();
                                }
                            }
                        }
                        // 上传图片
                        if (!item.isFormField())
                        {
                            InputStream in = null;
                            FileOutputStream out = null;
                            try
                            {
                                in = item.getInputStream();
                                out = new FileOutputStream(new File("E:/test.txt"));
    
                                byte[] buff = new byte[1024];
                                int len = 0;
                                while ((len = in.read(buff)) != -1)
                                {
                                    out.write(buff, 0, len);
                                }
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                            finally
                            {
                                if (in != null)
                                {
                                    try
                                    {
                                        in.close();
                                    }
                                    catch (IOException e)
                                    {
                                        e.printStackTrace();
                                    }
                                    finally
                                    {
                                        if (out != null)
                                        {
                                            try
                                            {
                                                out.close();
                                            }
                                            catch (IOException e)
                                            {
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                }
    
                            }
                        }
                    }
                }
                catch (FileUploadException e)
                {
                    e.printStackTrace();
                }
    
            }
    
        }
    }
    

      在Spring中不用配置:否则会导致request请求两次解析造成解析为空值的问题(详情可以参考:http://blog.csdn.net/lwphk/article/details/43015829)

    <!-- 支持上传文件 -->  
     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    		<property name="maxUploadSize" value ="1024" /> 
    		<property name="resolveLazily" value="true"/> 
            <property name="defaultEncoding" value = "UTF-8" /> 
    

      

  • 相关阅读:
    20080619 SQL SERVER 输入 NULL 的快捷键
    20090406 Adobe的“此产品的许可已停止工作”错误的解决办法
    20080908 Office Powerpoint 2007 不能输入中文的解决办法
    20080831 ClearGertrude Blog Skin 's cnblogs_code class
    20080603 Facebook 平台正式开放
    20080519 安装 Microsoft SQL Server 2000 时提示 创建挂起的文件操作
    test
    Linux—fork函数学习笔记
    SOA的设计理念
    Why BCP connects to SQL Server instance which start with account of Network Service fail?
  • 原文地址:https://www.cnblogs.com/byteworld/p/6004204.html
Copyright © 2011-2022 走看看