Servlet.service() for servlet [****.****] in context with path [/practice_system] threw exception

我的出错的源代码:

ImporMsg.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
	
</script>
<script>
	$(document).ready(function() {
		$("#submitfile").click(function() {
			$.post("/practice_system/Test1", {
				type : "1"
			}, function(data, status) {
				alert("数据: \n" + data + "\n状态: " + status);
			});
		});
	});
</script>
</head>
<body>
	<form action="ImportMsg" enctype="multipart/form-data" method="post">
		选择文件: <input type="file" name="excelfilename"><br> <input
			type="submit" id="submitfile" value="数据入库" />
	</form>
</body>
</html>

ImportMsg.java:

package interfaceServlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import databaseOrm.DBConnection;
import databaseOrm.DatabaseOperation;

/**
 * Servlet implementation class ImportMsg
 */
@WebServlet("/ImportMsg")
public class ImportMsg extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// 使用工具类连接数据库
	DatabaseOperation db = DBConnection.db;

	// 设置环境:创建一个DiskFileItemFactory工厂
	FileItemFactory factory = new DiskFileItemFactory();
	// 核心操作类:创建一个文件上传解析器
	ServletFileUpload upload = new ServletFileUpload(factory);

	static DatabaseOperation conn = DBConnection.db;
	DatabaseOperation stmt = DBConnection.db;

	static List items;
	static InputStream is = null;
	static Workbook workbook;
	static Sheet sheet;
	static int rows;
	static int columns;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

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

		String type = request.getParameter("type");

		try {
			// 使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List集合
			items = upload.parseRequest(request);
			// Iterator定义一个迭代器,是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象
			Iterator iter = items.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				if (!item.isFormField()) {// 判断一个参数域是普通的表单输入域,还是文件上传域,是后者的情况下,就要对相应的域做相应的文件上传处理
					is = item.getInputStream();
				}
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			workbook = Workbook.getWorkbook(is);
			sheet = workbook.getSheet(0);
			// 行数
			rows = sheet.getRows();
			System.out.println("行数:" + rows);
			// 列数
			columns = sheet.getColumns();
			System.out.println("列数:" + columns);
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// // 以列数判断读取的角色类型
		//
		// /**
		// * type: 1:学生 2:项目辅导老师 3:项目管理员
		// */
		// if (columns == 10) {
		// type = "1";
		// } else if (columns == 6) {
		// type = "2";
		// } else if (columns == 5) {
		// type = "3";
		// }

		System.out.println("type=" + type);

		switch (type) {
		case "1":
			// 插入学生信息
			insert_student(rows, sheet);
			System.out.println("插入学生信息");
			break;
		case "2":
			// 插入项目老师信息
			insert_teacher(rows, sheet);
			System.out.println("插入项目老师信息");
			break;
		case "3":
			// 插入项目管理员信息
			insert_manager(rows, sheet);
			System.out.println("插入项目管理员信息");
			break;
		default:
			System.out.println("错误type类型");
		}
	}

	/**
	 * 插入学生信息
	 * 
	 * @param rows
	 * @param sheet
	 */
	public static void insert_student(int rows, Sheet sheet) {
		try {
			for (int i = 0; i < rows; i++) {
				String sql = new String(
						"insert into student(sid,ssex,sname,spwd,sschool,smajor,sphone,sproject,spid,stid) values");
				if (i == 0) {// 第一行是属性,不读取
					continue;
				}
				Cell ce0 = ((jxl.Sheet) sheet).getCell(0, i);
				Cell ce1 = ((jxl.Sheet) sheet).getCell(1, i);
				Cell ce2 = ((jxl.Sheet) sheet).getCell(2, i);
				Cell ce3 = ((jxl.Sheet) sheet).getCell(3, i);
				Cell ce4 = ((jxl.Sheet) sheet).getCell(4, i);
				Cell ce5 = ((jxl.Sheet) sheet).getCell(5, i);
				Cell ce6 = ((jxl.Sheet) sheet).getCell(6, i);
				Cell ce7 = ((jxl.Sheet) sheet).getCell(7, i);
				Cell ce8 = ((jxl.Sheet) sheet).getCell(8, i);
				Cell ce9 = ((jxl.Sheet) sheet).getCell(9, i);

				String c0 = "'" + ce0.getContents() + "'";
				String c1 = "'" + ce1.getContents() + "'";
				String c2 = "'" + ce2.getContents() + "'";
				String c3 = "'" + ce3.getContents() + "'";
				String c4 = "'" + ce4.getContents() + "'";
				String c5 = "'" + ce5.getContents() + "'";
				String c6 = "'" + ce6.getContents() + "'";
				String c7 = "'" + ce7.getContents() + "'";
				String c8 = "'" + ce8.getContents() + "'";
				String c9 = "'" + ce9.getContents() + "'";

				sql = sql + "(" + c0 + "," + c1 + "," + c2 + "," + c3 + ","
						+ c4 + "," + c5 + "," + c6 + "," + c7 + "," + c8 + ","
						+ c9 + ")";
				System.out.println(sql);
				conn.update_op(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 插入项目老师信息
	 * 
	 * @param rows
	 * @param sheet
	 */
	public static void insert_teacher(int rows, Sheet sheet) {
		try {
			for (int i = 0; i < rows; i++) {
				String sql = new String(
						"insert into teacher(tid,tpwd,tsex,tname,tphone,tpid) values");
				if (i == 0) {// 第一行是属性,不读取
					continue;
				}
				Cell ce0 = ((jxl.Sheet) sheet).getCell(0, i);
				Cell ce1 = ((jxl.Sheet) sheet).getCell(1, i);
				Cell ce2 = ((jxl.Sheet) sheet).getCell(2, i);
				Cell ce3 = ((jxl.Sheet) sheet).getCell(3, i);
				Cell ce4 = ((jxl.Sheet) sheet).getCell(4, i);
				Cell ce5 = ((jxl.Sheet) sheet).getCell(5, i);

				String c0 = "'" + ce0.getContents() + "'";
				String c1 = "'" + ce1.getContents() + "'";
				String c2 = "'" + ce2.getContents() + "'";
				String c3 = "'" + ce3.getContents() + "'";
				String c4 = "'" + ce4.getContents() + "'";
				String c5 = "'" + ce5.getContents() + "'";

				sql = sql + "(" + c0 + "," + c1 + "," + c2 + "," + c3 + ","
						+ c4 + "," + c5 + ")";
				System.out.println(sql);
				conn.update_op(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 插入项目管理员信息
	 * 
	 * @param rows
	 * @param sheet
	 */
	public static void insert_manager(int rows, Sheet sheet) {
		try {
			for (int i = 0; i < rows; i++) {
				String sql = new String(
						"insert into projector(pid,ppwd,psex,pphone,pname) values");
				if (i == 0) {// 第一行是属性,不读取
					continue;
				}
				Cell ce0 = ((jxl.Sheet) sheet).getCell(0, i);
				Cell ce1 = ((jxl.Sheet) sheet).getCell(1, i);
				Cell ce2 = ((jxl.Sheet) sheet).getCell(2, i);
				Cell ce3 = ((jxl.Sheet) sheet).getCell(3, i);
				Cell ce4 = ((jxl.Sheet) sheet).getCell(4, i);

				String c0 = "'" + ce0.getContents() + "'";
				String c1 = "'" + ce1.getContents() + "'";
				String c2 = "'" + ce2.getContents() + "'";
				String c3 = "'" + ce3.getContents() + "'";
				String c4 = "'" + ce4.getContents() + "'";

				sql = sql + "(" + c0 + "," + c1 + "," + c2 + "," + c3 + ","
						+ c4 + ")";
				System.out.println(sql);
				conn.update_op(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

运行出现错误:

自己写的一个简易的文件上传的前后端,点击上传时出现了这个错误,当时一直不知道原因。

 

 

后来才知道我在一个表单中提交本地文件时,表单form的enctype="multipart/form-data",是以二进制数据流提交数据的。
所以没法用request.getParameter("name")来获取提交到后台的普通表单域值

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值