📄 bookaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.xfaccp.bookonline.struts.action;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import com.xfaccp.bookonline.hib.dao.BookTableDAO;
import com.xfaccp.bookonline.hib.dto.BookTable;
/**
* MyEclipse Struts Creation date: 09-26-2007
*
* XDoclet definition:
*
* @struts.action path="/book" name="bookForm" parameter="type" scope="request"
* validate="true"
*/
public class BookAction extends DispatchAction {
/*
* Generated Methods
*/
/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward xiugai(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
BookTable book = (BookTable) form;
System.out.println(book.getBookid());
FormFile file = book.getFile();
// If no file was uploaded (e.g. first form load), then display View
if (file == null) {
return mapping.findForward("success");
}
// Get the name and file size
String fname = file.getFileName();
this.uploadMethod(file);
// Clean up our toys when done playing
file.destroy();
String[] s = fname.split("[.\n]");
if (s.length > 0) {
if (s[s.length - 1].equals("gif") || s[s.length - 1].equals("jpg")) {
book.setPicture(fname);
}
} else {
return mapping.findForward("fail");
}
this.uploadMethod(file);
BookTableDAO dao = new BookTableDAO();
dao.updateMethod(book);
System.out.println(book.getBookname());
return mapping.findForward("success");
}
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
BookTable hff = (BookTable) form;
// org.apache.struts.upload.FormFile contains the uploaded file
FormFile file = hff.getFile();
// If no file was uploaded (e.g. first form load), then display View
if (file == null) {
return mapping.findForward("success");
}
// Get the name and file size
String fname = file.getFileName();
this.uploadMethod(file);
// Clean up our toys when done playing
file.destroy();
String[] s = fname.split("[.\n]");
if (s.length > 0) {
if (s[s.length - 1].equals("gif") || s[s.length - 1].equals("jpg")) {
hff.setPicture(fname);
}
} else {
return mapping.findForward("fail");
}
BookTableDAO dao = new BookTableDAO();
if (dao.save(hff)) {
return mapping.findForward("success");
} else {
return mapping.findForward("fail");
}
// Forward to default display
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String id = request.getParameter("id");
BookTableDAO dao = new BookTableDAO();
BookTable book = dao.findById(new Integer(id));
dao.delete(book);
System.out.println("execute delete method" + book.getBookid());
return mapping.findForward("success");
}
public void uploadMethod(FormFile file) {
String dir = servlet.getServletContext().getRealPath("/upload");
try {
String fname = file.getFileName();
InputStream streamIn = file.getInputStream();
OutputStream streamOut = new FileOutputStream(dir + "/" + fname);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
// Populate the form bean with the results for display in the View
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -