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>上传图片功能</title>
</head>
<body>
    <form action="a.do" 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"/> 
    	</div>
    </form>
</body>
</html>

Servlet接收参数和文件的代码如下:

package test;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class A extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect("abc.jsp");
	}
	
	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();
			// 得到上传的文件对象
			com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0);
			String name = smartFile.getFileName();
			// 保存文件
			smartFile.saveAs("/picture/"+name, SmartUpload.SAVE_AUTO);
			// 传过来的注册数据
			// 只需要new SmartUpload().getRequest().getParameter(""))就能获取到相应的表单数据
			String stuname = smartUpload.getRequest().getParameter("stuname");
			int stuno = Integer.parseInt(smartUpload.getRequest().getParameter("stuno"));
			System.out.println("name:  "+stuname+"   stuno:  "+stuno);
			response.sendRedirect("abc.jsp");
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

注意:  1.servlet代码去掉包名,换成自己的。

            2.需要在自己的WebContent目录下创建picture文件。

            3.导入smartupload.jar包

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值