JspSmart中同时上传文件与表单信息

来源:互联网 发布:手机归属地数据库下载 编辑:程序博客网 时间:2024/04/28 23:38

实例

register.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=gb2312"%><%@ page import="dao.StudentDao,bean.student"%><%   String path = request.getContextPath();   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   request.setCharacterEncoding("gb2312");//防止中文乱码   %><html><body>    <form action="/实验8/servlet/UploadServlet" method="post" enctype="multipart/form-data">        <div align="center" >            <label>姓名: </label>            <input name="stuname"  type="text"/>             <label>学号: </label>            <input name="stuno"  type="text"/>            <label>年龄: </label>            <input name="age"  type="text"/>             <label>电话号码: </label>            <input name="phone"  type="text"/>            <br><br>            <label>性别: </label>            <input name="sex"  type="radio" value="男" checked/><input name="sex"  type="radio" value="女"/><br><br>            <label>照片: </label>            <input name="picture" value="" type="file"/>            <br><br>            <input value="注册" type="submit"/>     </form>            <br>        <hr></body></html>

UploadServlet.java 部分代码

public class UploadServlet extends HttpServlet {    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {        //初始化    SmartUpload smartUpload = new SmartUpload();    ServletConfig config = this.getServletConfig();    smartUpload.initialize(config, request, response);    try {            //上传文件            smartUpload.upload();            //得到上传的文件对象            File smartFile = smartUpload.getFiles().getFile(0);            String name = smartFile.getFileName();            //保存文件            smartFile.saveAs("/picture/"+name, smartUpload.SAVE_VIRTUAL);            //传过来的注册数据            //只需要new SmartUpload().getRequest().getParameter(""))就能获取到相应的表单数据    stuname = smartUpload.getRequest().getParameter("stuname");    stuno = Integer.parseInt(smartUpload.getRequest().getParameter("stuno"));                    .......        } catch (SmartUploadException e) {            // TODO Auto-generated catch block            e.printStackTrace();            } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}
0 0
原创粉丝点击