⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 updateservlet.java

📁 手工完成
💻 JAVA
字号:
package Servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;

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

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

public class UpdateServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public UpdateServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//------------------------Start MyCode----------------------------------

		System.out.println("Have enter BookUpdateServlet");
		ServletContext application = getServletContext();
		Bean.BookBean book = new Bean.BookBean();   //建立一个book;
		try {
			boolean isMultipart = ServletFileUpload.isMultipartContent(request); //静态方法

			if (!isMultipart) //判断是表单的MIME是否为mutilpart/form-dat。  是返回true,否则返回false
			{
				request.getRequestDispatcher("error.jsp").forward(request,response);
			}
	//		 book = new Bean.BookBean();   //建立一个book

			DiskFileItemFactory factory = new DiskFileItemFactory();//1用来设置阙值  2设置保存的路径
			factory.setSizeThreshold(1024);
//			factory.setRepository("D:\\Img");
//			factory.setRepository();

			ServletFileUpload upload = new ServletFileUpload(factory);
			upload.setSizeMax(1024 * 1024);
			 	
			ArrayList<FileItem> fileItems = (ArrayList<FileItem>) upload.parseRequest(request);
		
			Iterator<FileItem> it = fileItems.iterator();

			String uploadPath = application.getRealPath("/");//把上传的内容放到当前web应用目录下
//			String uploadPath = "D://my/"; //把上传的内容放到D盘  注意:D盘必须有着这文件夹
			int location = -1;

			while (it.hasNext()) {

				FileItem fi = (FileItem) it.next();
				if (fi.isFormField()) //这if 语句用来判断时候是表单元素  并且插入到一个User中
				{
					System.out.println("进入表单元素!");
					if (fi.getFieldName().equals("bookname")) {
						book.setBookName(new String(fi.getString().getBytes("ISO-8859-1"), "GB2312"));
					} else if (fi.getFieldName().equals("author")) {
						book.setAuthor(new String(fi.getString().getBytes("ISO-8859-1"), "GB2312"));
					} else if (fi.getFieldName().equals("price")) {
						book.setPrice(Double.parseDouble(new String(fi.getString().getBytes("ISO-8859-1"),"GB2312")));
					} else if (fi.getFieldName().equals("discount")) {
						book.setDiscount(Double.parseDouble(new String(fi.getString().getBytes("ISO-8859-1"),"GB2312")));
					} else if (fi.getFieldName().equals("press")) {
						book.setPress(new String(fi.getString().getBytes("ISO-8859-1"), "GB2312"));
					} else if(fi.getFieldName().equals("sum")){
						book.setSum(Integer.parseInt(new String(fi.getString().getBytes("ISO-8859-1"),"GB2312")));
					}else if(fi.getFieldName().equals("bookID")){
						book.setBookID(new String(fi.getString().getBytes("ISO-8859-1"),"GB2312"));
					}
				}

				String oriFileName = fi.getName();
				System.out.println("orifilename is " + oriFileName);

				if (oriFileName != null) {
					location = oriFileName.lastIndexOf("\\");
					System.out.println("location is " + String.valueOf(location));
				}

				if (!fi.isFormField() && oriFileName != null && location >= 0)//判断是否进入文件上传的元素
				{
//---Start 文件上传---					
					String fileName = oriFileName.substring(location + 1);
System.out.println("这是BookID号<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<:" + book.getBookID());
					fileName = book.getBookID() + fileName; //这行可以给上传的文件加上ID,以保证没有相同的冲突!
					System.out.println("fileName is " + fileName);
					File saveFile = new File(uploadPath+ "/BookImg/",fileName);
					System.out.println("这个是上传文件的保存路径-------------------------------------------------");
					System.out.println(uploadPath + "\\Img\\" + fileName);
					fi.write(saveFile);
					System.out.println("file succeedd!!!");
					System.out.println("file size" + fi.getSize());
					book.setPicroot("BookImg" + "\\" + fileName);
//---End----
				}

			}
	
			
//---Start--下面用来把bookBean中的内容插入到数据库中---
			Bean.BookUpdate bookupdate = new Bean.BookUpdate();
			bookupdate.Update(book);
//---End---
			//	request.getRequestDispatcher("/FileUpLoadSucceed.jsp").forward(request, response);
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("book", book);
		request.setAttribute("errormsg", "成功上传!");
		request.getRequestDispatcher("/ShowDelUpdateBook.jsp").forward(request, response);
		//----------------------End MyCode--------------------------------------
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -