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

实例

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();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值