?? updateservlet.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 + -