jspsmartupload 文件上传让input数据和文件上传同时提交
2016-03-19 15:13
507 查看
一、使用原因:
文件上传时,表单的属性中必须要有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");
相关文章推荐
- 研究了三天的文件上传(jspsmart实现)file和text表单同时提交的问题
- JSPSmart实现文件上传时file和text表单同时提交的问题
- 文件上传(jspsmart实现)file和text表单同时提交的问题
- 使用jspsmartupload解决enctype引起的无法同时上传文件和接值的问题
- 用jspSmartUpload上传文件同时获得参数的方法。
- 研究了三天的文件上传(jspsmart实现)file和text表单同时提交的问题
- JSP SMARTUPLOAD组件:上传文件时同时获取表单参数
- 文件上传(jspsmart实现)file和text表单同时提交的问题
- 我遇到的问题JspSmartUpload组件实现表单和文件同时上传
- SmartUpload实现文件上传时file和表单文本同时提交的问题
- 如何用jspSmartUpload上传文件:)
- 【Jsp】使用jspsmartupload完成简单的文件上传系统
- jsp+servlet文件上传以及request提交数据利用反射封装到实力类中
- smartupload实现文件上传 并获取表单数据
- [JSP]jspsmartupload完成简单的文件上传
- JspsmartUpload上传文件(1)
- jspsmart实现文件上传下载及jspSmartUpload.jar下载
- 今天使用jspsmartupload.jar实现上传文件的功能,发现中文乱码,于是总结了下解决方法
- jspsmartupload 乱码问题和文件上传下载
- JSP Smartupload form同时提交file 和 text