package com.sasis.webapp.action;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
import java.net.URLEncoder;
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.upload.FormFile;
import com.sasis.model.FileManager;
import com.sasis.model.User;
import com.sasis.service.FileManagerManager;
import com.sasis.webapp.form.FileManagerForm;
public class FileManagerAction extends BaseAction{
public ActionForward fileManager(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response)throws Exception{
return mapping.findForward("file_manager");
}
public ActionForward uploadFile(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response)throws Exception{
FileManagerForm fmf = (FileManagerForm)form;
String sort = request.getParameter("sort");
Hashtable files = form.getMultipartRequestHandler().getFileElements();
FileManagerManager fmm = (FileManagerManager)getBean("fileManagerManager");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
FileManager fileManager;
Iterator it = files.keySet().iterator();
//String dir = this.getServlet().getServletContext().getRealPath("upload")+"\\";
String basePath = getFilePath()+"\\";
String fileName;
long timeMillis;
User user = (User)request.getSession().getAttribute("user");
while(it.hasNext()){
timeMillis = System.currentTimeMillis();
String key = (String)it.next();
FormFile file = (FormFile)files.get(key);
fileName = file.getFileName();
String savePath = basePath+timeMillis+fileName;
OutputStream os = null;
InputStream is = null;
if(file!=null && file.getFileSize()>0){
try{
is = file.getInputStream();
byte b[] = new byte[1024] ;
int length = 0;
os = new FileOutputStream(savePath);
while((length=is.read(b))>0){
os.write(b, 0, length);
}
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
is.close();
os.close();
}
fileManager = new FileManager();
fileManager.setTitle(fmf.getTitle());
fileManager.setFilePath(savePath);
fileManager.setUploadDate(new Date());
fileManager.setUploadUser(user.getCHName());
fileManager.setExpirdDate(sdf.parse(fmf.getExpirdDate()));
if(sort!=null && sort.equals("2")){
fileManager.setSort("2");
fileManager.setFlightDate(sdf.parse(fmf.getFlightDate()));
fileManager.setFlightNum(fmf.getFlightNum());
}else{
fileManager.setSort("1");
}
fmm.saveFile(fileManager);
}
}
System.out.println(basePath);
request.setAttribute("message", "success");
return mapping.findForward("file_add");
}
public ActionForward downloadFile(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response)throws Exception{
String fileId = request.getParameter("fid");
FileManagerManager fmm = (FileManagerManager)getBean("fileManagerManager");
FileManager fm = fmm.getFileById(fileId);
String filePath="",loadTitle="" ;
if(fm!=null){
filePath = fm.getFilePath();
loadTitle = fm.getTitle()+filePath.substring(filePath.indexOf("."),filePath.length());
}else{
PrintWriter out = response.getWriter();
out.write("The file is not exist....");
return null;
}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
InputStream is = null;
OutputStream os = null;
try{
response.setHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(loadTitle,"utf-8"));
File file = new File(filePath);
System.out.println(file.length());
is = new FileInputStream(file);
bis = new BufferedInputStream(is);
os = response.getOutputStream();
bos = new BufferedOutputStream(os);
byte[] buffer = new byte[8192];
int length = 0 ;
while((length=bis.read(buffer))>0){
bos.write(buffer,0,length);
}
bos.flush();
}catch(IOException e){
e.printStackTrace();
return null;
}finally{
is.close();
bis.close();
os.close();
bos.close();
}
return null;
}
public ActionForward deleteFile(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response)throws Exception{
String arrId = request.getParameter("arrId");
String ids[] = arrId.split(",");
FileManager fm;
FileManagerManager fmm = (FileManagerManager)getBean("fileManagerManager");
if(ids.length>0){
for(int i=0;i<ids.length;i++){
fmm.deleteFile(Integer.parseInt(ids[i]));
}
}
sortFlag = request.getParameter("sort");
return queryFile(mapping,form,request,response);
}
String sortFlag;
public ActionForward queryFile(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response)throws Exception{
String sort = request.getParameter("sort");
if(sort==null)
sort = sortFlag;
String onlyQuery = request.getParameter("onlyQuery");
FileManagerForm fmf = (FileManagerForm)form;
List fileList = new ArrayList();
FileManagerManager fmm = (FileManagerManager)getBean("fileManagerManager");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
FileManager fileManager = new FileManager();
fileManager.setTitle(fmf.getFileName());
fileManager.setUploadDateStr(fmf.getUploadDate());
fileManager.setFlightDateStr(fmf.getFlightDate());
fileManager.setFlightNum(fmf.getFlightNum());
Date today = new Date();
if(sort!=null)
fileManager.setSort("2");
else
fileManager.setSort("1");
if(onlyQuery != null){
fileManager.setCompareToday(sdf.format(today));
}
fileList = fmm.getFilesList(fileManager);
if(onlyQuery==null && fileList != null && fileList.size()>0){
for(int i=0;i<fileList.size();i++){
fileManager = (FileManager)fileList.get(i);
if(sdf.format(fileManager.getExpirdDate()).compareTo(sdf.format(today))<0){
fileManager.setIsOutOfTime("1");
}
}
}
request.setAttribute("fileList", fileList);
sortFlag = null;
if(onlyQuery!=null && onlyQuery.equals("1"))
return mapping.findForward("file_query");
if(sort!=null)
return mapping.findForward("cabin_file_manager");
return mapping.findForward("file_manager");
}
public ActionForward goFileAdd(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response)throws Exception{
String sort = request.getParameter("sort");
if(sort!=null)
return mapping.findForward("cabin_file_add");
return mapping.findForward("file_add");
}
public ActionForward viewFileInfo(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response)throws Exception{
String fileId = request.getParameter("fid");
String sort = request.getParameter("sort");
FileManagerManager fmm = (FileManagerManager)getBean("fileManagerManager");
FileManager fileManager = fmm.getFileById(fileId);
request.setAttribute("fileManager", fileManager);
if(sort!=null)
return mapping.findForward("cabin_view_file_info");
return mapping.findForward("view_file_info");
}
public ActionForward modifyFile(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response)throws Exception{
FileManagerForm fmf = (FileManagerForm)form;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
FileManagerManager fmm = (FileManagerManager)getBean("fileManagerManager");
FileManager fm = new FileManager();
fm.setTitle(fmf.getFileName());
fm.setExpirdDate(sdf.parse(fmf.getExpirdDate()));
fm.setFlightDate(fmf.getFlightDate()==null||fmf.getFlightDate().equals("")?null:sdf.parse(fmf.getFlightDate()));
fm.setFlightNum(fmf.getFlightNum());
fm.setId(Integer.parseInt(fmf.getId()));
fmm.updateFileInfo(fm);
request.setAttribute("message", "success");
return mapping.findForward("view_file_info");
}
public ActionForward unspecified(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response)throws Exception{
String sort = request.getParameter("sort");
if(sort!=null)
return mapping.findForward("cabin_file_frame");
return mapping.findForward("file_frame");
}
public String getFilePath(){
InputStream is = this.getClass().getClassLoader().getResourceAsStream("up_down_loadfile.properties");
Properties p = new Properties();
try{
p.load(is);
}catch(Exception e){
e.printStackTrace();
}
return p.getProperty("file.savedir");
}
}