-
-
-
-
- package com.action;
-
- import com.form.FileForm;
- import javax.servlet.http.*;
- import org.apache.struts.action.*;
- import org.apache.struts.actions.DispatchAction;
- import java.util.Date;
- import java.util.Calendar;
- import java.util.Vector;
- import org.apache.struts.upload.*;
- import tools.ListPage;
- import java.io.*;
- import java.net.URLEncoder;
-
-
-
-
-
-
-
-
- public class FileAction extends DispatchAction {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public ActionForward upload(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- final String fileExt="gif,GIF,jpg,JPG,jpeg,JPEG";
- final int maxSize=409600;
- FileForm fileForm = (FileForm) form;
- ActionMessages messages=new ActionMessages();
- boolean flag=true;
- if(fileForm.getName().equals(""))
- {
- flag=false;
- messages.add("nameerror",new ActionMessage("name.error"));
- }
- if(fileForm.getUser().equals(""))
- {
- flag=false;
- messages.add("usererror",new ActionMessage("user.error"));
- }
- if(fileForm.getFile().getFileSize()==0)
- {
- flag=false;
- messages.add("fileerror",new ActionMessage("file.error"));
- }
- else
- {
- FormFile f=fileForm.getFile();
- int fileSize=f.getFileSize();
- int i=f.getFileName().indexOf(".");
- String ext=f.getFileName().substring(i+1);
- if(fileExt.indexOf(ext)==-1)
- {
- flag=false;
- messages.add("fileexterror",new ActionMessage("file.ext.error"));
- }
- if(fileSize>maxSize)
- {
- flag=false;
- messages.add("filesizeerror",new ActionMessage("file.size.error"));
- }
- }
- HttpSession session=request.getSession();
- if(request.getParameter("validate").equals(""))
- {
- flag=false;
- messages.add("validatenull",new ActionMessage("validate.null.error"));
- }
- else if(!request.getParameter("validate").equals((String)session.getAttribute("valicode")))
- {
- flag=false;
- messages.add("validateerror",new ActionMessage("validate.error"));
- }
- if(!flag)
- {
- saveErrors(request,messages);
- return mapping.findForward("error");
- }
- FormFile file=fileForm.getFile();
- String dir=servlet.getServletContext().getRealPath("/");
- int size=file.getFileSize();
-
- String fileName=uploadFile(dir,file);
- String path="/"+fileName;
- fileForm.setPath(path);
- fileForm.setSize(file.getFileSize());
- fileForm.setTime(new Date().toLocaleString());
- ListPage listPage=new ListPage();
- listPage.insert(fileForm);
-
-
-
-
-
-
- messages.add("filesuccess",new ActionMessage("file.success"));
- saveMessages(request,messages);
- return mapping.findForward("success");
- }
-
-
- public ActionForward download(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- {
- String path=request.getParameter("path");
- BufferedInputStream bis=null;
- BufferedOutputStream bos=null;
- OutputStream os=null;
- InputStream is=null;
- String filePath=servlet.getServletContext().getRealPath("/"+path);
- try
- {
-
- File downloadFile=new File(filePath);
- is=new FileInputStream(downloadFile);
- bis=new BufferedInputStream(is);
- os=response.getOutputStream();
- bos=new BufferedOutputStream(os);
- response.setHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(path,"utf-8"));
- int bytesRead=0;
- byte[] buffer=new byte[8192];
- while((bytesRead=bis.read(buffer,0,8192))!=-1)
- {
- bos.write(buffer, 0, bytesRead);
- }
- bos.flush();
- is.close();
- bis.close();
- os.close();
- bos.close();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return null;
- }
-
- public String uploadFile(String dir,FormFile file)
- {
- String fname=file.getFileName();
- int i=fname.indexOf(".");
- String type=fname.substring(i+1);
- Calendar calendar = Calendar.getInstance();
- String fileName="files/"+calendar.get(Calendar.YEAR) + (calendar.get(Calendar.MONTH)+1) + calendar.get(Calendar.DAY_OF_MONTH) +calendar.get(Calendar.HOUR_OF_DAY)+calendar.get(Calendar.MINUTE)+calendar.get(Calendar.SECOND)+calendar.get(Calendar.MILLISECOND)+Math.round(Math.random()*100*1000)+"."+type;
-
- try
- {
- InputStream in=file.getInputStream();
- String path=dir+fileName;
- OutputStream out=new FileOutputStream(path);
- int bytesRead=0;
- byte[] buffer=new byte[8192];
- while((bytesRead=in.read(buffer, 0, 8192))!=-1)
- {
- out.write(buffer, 0, bytesRead);
- }
- out.close();
- in.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- file.destroy();
- return fileName;
- }
-
- }
|