jspsmartupload 文件上传让input数据和文件上传同时提交

一、使用原因:

文件上传时,表单的属性中必须要有multipart/form-data,如以下例子:

<form name="form_post" class="am-form am-form-horizontal"

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

但是加上该属性以后,表单中所有数据都会以二进制的形式上传,表单中的input类型数据就无法上传。此时就要用到jspsmartupload这个组件,可以确保同时提交两种数据。

二、使用方法:

1.下载jspsmartupload.jar包,导入到该项目中。

2.在文件上传的servlet中如下写。

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        request.setCharacterEncoding("gbk");

        /*---------------------------------------------文件上传----------------------------------*/
        SmartUpload smart=new SmartUpload();

        //PageContext是jsp的内置对象,在servlet不能直接使用,需要做一些处理
        JspFactory _jspxFactory = null;
        PageContext pageContext = null;
        _jspxFactory = JspFactory.getDefaultFactory();
        pageContext = _jspxFactory.getPageContext(this,request,response,"",true,8192,true);

        smart.initialize(pageContext);//初始化上传操作
        try {
            smart.upload();
        } catch (SmartUploadException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        IpTimeStamp its=new IpTimeStamp(InetAddress.getLocalHost().getHostAddress());//request.getRemoteAddr()获得用户的ip地址
        //System.out.println("获取的ip为"+InetAddress.getLocalHost().getHostAddress());
        //如果要实现文件的批量上传,则只需用for循环,将getFile(0)中的0改为i即可
        String ext=smart.getFiles().getFile(0).getFileExt();//此为得到文件的扩展名,getFile(0)为得到唯一的一个上传文件
        String fileName=its.getIpTimeRand()+"."+ext;
        //System.out.println("获取 的文件名为"+fileName);
        //this.getServletContext().getRealPath("/")为得到tomcat的跟目录,放于upload文件夹中,java.io.File.separator是一种安全操作
        //String realPath="";
        //this.getServletContext().getRealPath("/")+
        try {
            smart.getFiles().getFile(0).saveAs(".\\WEB-INF\\upload\\"+java.io.File.separator+fileName);
        } catch (SmartUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String realPath="D:\\Program Files (x86)\\tomcat\\webapps\\MyData\\WEB-INF\\upload"+fileName+"";

        /*-------------------------接受表单数据---------------------------------------------------*/    

        String title=smart.getRequest().getParameter("title");
        String range=smart.getRequest().getParameter("range");
        String time=smart.getRequest().getParameter("time");
        String source=smart.getRequest().getParameter("source");
        String area=smart.getRequest().getParameter("area");
        String content=smart.getRequest().getParameter("content");
        /*ChangeEncoder cn=new ChangeEncoder();
        title=cn.getUTF8StringFromGBKString(title);
        source=cn.getUTF8StringFromGBKString(source);
        area=cn.getUTF8StringFromGBKString(area);
        content=cn.getUTF8StringFromGBKString(content);
        System.out.println(title);
        System.out.println(range);
        System.out.println(time);
        System.out.println(source);
        System.out.println(area);*/
}

三、注意事项

1默认的jspsmartupload是以gbk的形式上传的数据,所以在文件上传的jsp页面中设置编码为gbk,在servlet中设置接收的编码也为gbk.否则会出现乱码。

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

request.setCharacterEncoding("gbk");

posted @ 2016-03-19 15:13  岁月静好--lyr  阅读(1468)  评论(0编辑  收藏  举报