?? fileuploadaction.java
字號:
package bit.jeffy.action;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import bit.jeffy.db.DataStore;
import bit.jeffy.personal.FileUploadBean;
/*
* 創(chuàng)建日期 2005-12-8
*
* TODO 要更改此生成的文件的模板,請轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板
*/
/**
* @author jeffy
*
* TODO 要更改此生成的類型注釋的模板,請轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板
*/
public class FileUploadAction extends Action {
/* (非 Javadoc)
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public ActionForward execute(ActionMapping map, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO 自動生成方法存根
HttpSession session = request.getSession();
ActionErrors errors = new ActionErrors();
String ZH = (String)session.getAttribute("current_user");
if( ZH == null ){
ZH = "default";
}else {
ZH = new String(ZH.getBytes("ISO-8859-1"),"GB2312");
}
//取得當(dāng)前servlet的全路徑,然后在其路徑上加上/file/'ZH'
String dir = servlet.getServletContext().getRealPath("/file/"+ZH);
FileUploadBean hff = (FileUploadBean)form;
FormFile file = hff.getFile();
if( file == null ){
errors.add("fail_1",new ActionError("FileUploadAction.error.a"));
saveErrors(request,errors);
return (map.findForward("uploadfail"));
}
String fname = file.getFileName();
float filesize = (float)(file.getFileSize()/1024.0);
//判斷用戶空間是否已滿
DataStore ds = DataStore.getInstance();
ResultSet rs = null;
String sql = "select * from ClientFileSpace where ZH='"+ZH+"'";
float space = 0;
float exist = 0;
try{
if( ds != null ){
rs = ds.read(sql);
if( rs != null && rs.next() ){
space = rs.getFloat("FILESPACE");
exist = rs.getFloat("FILEEXIST");
if( (exist+filesize)> space ){
errors.add("fail_2",new ActionError("FileUploadAction.error.b"));
saveErrors(request,errors);
return (map.findForward("uploadfail"));
}
}
}
}catch(SQLException e){
errors.add("fail_3",new ActionError("FileUploadAction.error.c"));
saveErrors(request,errors);
return (map.findForward("uploadfail"));
}
//先執(zhí)行數(shù)據(jù)庫的操作,再執(zhí)行文件操作,若文件操作失敗, 則滾回?cái)?shù)據(jù)庫
String sql_0 = "select * from ClientFile where ZH='"+ZH+"' and FILENAME='"+fname+"'";
float origin_size = 0;
boolean if_exist = false;
try{
rs = ds.read(sql_0);
if( rs != null && rs.next() ){
if_exist = true;
origin_size = rs.getFloat("FILESIZE");
}
}catch(Exception e){
origin_size = 0;
errors.add("fail_4",new ActionError("FileUploadAction.error.d"));
saveErrors(request,errors);
return (map.findForward("uploadfail"));
}
String sql_1 = "insert ClientFile values('"+ZH+"','"+fname+"','"+filesize+
"',CURDATE(),'0')";
String sql_2 = "update ClientFileSpace " +
"set FILEEXIST=FILEEXIST+"+filesize+
",FILECOUNT=FILECOUNT+1 "+
"where ZH='"+ZH+"'";
String sql_3 = "update ClientFile "+
"set FILESIZE='"+filesize+"' "+
"where ZH='"+ZH+"' and FILENAME='"+fname+"'";
String sql_4 = "update ClientFileSpace " +
"set FILEEXIST=FILEEXIST+"+(filesize-origin_size)+
"where ZH='"+ZH+"'";
rs = null;
ds.beginTransaction();
try{
if( if_exist ){
ds.execute(sql_3);
ds.execute(sql_4);
}else{
ds.execute(sql_1);
ds.execute(sql_2);
}
}catch(Exception e){
ds.ErrorOccur();
ds.commitTransaction();
errors.add("fail_5",new ActionError("FileUploadAction.error.e"));
saveErrors(request,errors);
return (map.findForward("uploadfail"));
}
InputStream streamIn = null;
OutputStream streamOut = null;
try{
streamIn = file.getInputStream();
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();
}catch(Exception e){
ds.ErrorOccur();
ds.commitTransaction();
errors.add("fail_6",new ActionError("FileUploadAction.error.f"));
saveErrors(request,errors);
return (map.findForward("uploadfail"));
}
//執(zhí)行最后正確的事務(wù)
ds.commitTransaction();
file.destroy();
return (map.findForward("uploadsuccess"));
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -