struts 1.xマルチファイルアップロードを実現します.1.0(メモ)


もっと読む
               ,    ,                  。
1.    :
 
   var t = 1; 
   function addFileInput() 
   { 
    var parent = document.getElementById("more"); 

    var br = document.createElement("br"); 
    var input = document.createElement("input"); 
    var button = document.createElement("input"); 

    input.type = "file"; 
    input.name = "uploadFile[" + (t++) + "].file"; 
    input.size = "30"; 
    button.type = "button"; 
    button.value = "  "; 

    button.onclick = function() 
    { 
     parent.removeChild(br); 
     parent.removeChild(input); 
     parent.removeChild(button); 
    
    }; 

    parent.appendChild(br); 
    parent.appendChild(input); 
    parent.appendChild(button); 
   } 
 




				
				
			

  
 2.FileAction.java
	/**
	 *       1.0
	 */
	public ActionForward addFile(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response){
		FileForm fileForm = (FileForm)form;
		List uploadFiles =  fileForm.getFormFiles();
		// result = 0;
		if(uploadFiles != null){
			String root=request.getSession(true).getServletContext().getRealPath("/heyjdemo/upfiles/"); 
			FileUploadTool fileUploadTool = new FileUploadTool();
			fileUploadTool.saveFile(uploadFiles, root);
			
			/*
			 *          
			 */
			
		}
		

		return mapping.findForward("success"); 
	}
 
 
 
3.FileUploadTool.java
 
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts.upload.FormFile;

/**
 * 

: , upifles , * , 2011-3 :/upfiles/2010_3/ *

* *

: * * , : * , , uploadFile , : *

*

* *

: 1. , , , , ( ) * 2. * 3. ( ) ************************* * 4. , *********************** * *

* *

Create Date:2011-3-14

* */ public class FileUploadTool { /** * */ private String fname = ""; private String furl = ""; private String ftype = ""; private float fsize = 0; /** * */ private List fnames; private List furls; private List ftypes; private List fsizes; public String getFname() { return fname; } public String getFurl() { return furl; } public String getFtype() { return ftype; } public float getFsize() { return fsize; } public List getFnames() { return fnames; } public List getFurls() { return furls; } public List getFtypes() { return ftypes; } public List getFsizes() { return fsizes; } /** * * + * @return */ public String getUniqueString(){ Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currrentDateStr = simpleDateFormat.format(date); int randInt = (int)( Math.random()*10000); String uniqueStr = (currrentDateStr + randInt).replaceAll(" ", "") .replaceAll(":", "").replaceAll("-", ""); return uniqueStr; } /** * , * 1 , , 0 , * @param file * @param preFurl * @param preFname * @return */ public int saveFile(FormFile file, String root, String preFname){ // try{ fsize = file.getFileSize(); /** * 0 */ if(0 == fsize){ return 0; } String[] tempStr = preFname.split("\\."); if(tempStr.length>0){ ftype = tempStr[tempStr.length - 1 ]; } fname = getUniqueString(); furl = root + "\\" + fname; if(!"".equals(ftype)){ furl += "." + ftype; } InputStream in = file.getInputStream(); System.out.println(furl); OutputStream out = new FileOutputStream(furl); int read = 0; //fsize = 0; byte[] buffer = new byte[1024]; while((read = in.read(buffer,0,1024)) != -1){ out.write(buffer, 0, read); // fsize += read; } in.close(); out.close(); }catch(Exception e){ e.printStackTrace(); return 0; } ///fsize /= 1024;// kb return 1; } /** * * * @param files * @param url */ public void saveFile(List files, String root){ /* * */ fnames = new ArrayList(); furls = new ArrayList(); ftypes = new ArrayList(); fsizes = new ArrayList(); /* * */ for(UploadFile file : files){ FormFile formFile = file.getFile(); if(formFile == null){ continue; } saveFile(formFile,root,formFile.getFileName()); fnames.add(fname); furls.add(furl); ftypes.add(ftype); fsizes.add(fsize); } } /** * * @param furl */ public boolean deleteFile(String furl){ java.io.File file = new java.io.File(furl); if(file.exists()){ file.delete(); return true; }else{ return false; } } /** * * @param furls */ public List deleteFile(List furls){ List results = new ArrayList(); for(String furl : furls){ results.add(deleteFile(furl)); } return results; } public void cleanRubbishFiles(){ } }
 
 
4.FileForm.java
 
/**
 * 

: DAO

* *

: * , : * , , uploadFile , : *

*

* *

* *

Create Date:2011-3-14

* */ public class FileForm extends ActionForm { private File file = new File(); private List files; private Page page = new Page(1,3); /** * Struts */ private FormFile formFile; /** * * @return */ private List formFiles = new ArrayList(); public File getFile() { return file; } public void setFile(File file) { this.file = file; } public List getFiles() { return files; } public void setFiles(List files) { this.files = files; } public Page getPage() { return page; } public void setPage(Page page) { this.page = page; } public FormFile getFormFile() { return formFile; } public void setFormFile(FormFile formFile) { this.formFile = formFile; } public List getFormFiles() { return formFiles; } public void setFormFiles(List formFiles) { this.formFiles = formFiles; } public FileForm(){ } /** * , * struts1 , ----》list , * @param index * @return */ public UploadFile getUploadFile(int index) { int size = formFiles.size(); if (index > size - 1) { for (int i = 0; i < index - size + 1; i++) { formFiles.add(new UploadFile()); } } return (UploadFile) formFiles.get(index); } }
 
 
5.UploadFile.java
import java.io.Serializable;

import org.apache.struts.upload.FormFile;

/**
 * 

: struts1 formfile

* *

* *

* *

Create Date:2011-3-15

* */ public class UploadFile implements Serializable { private FormFile file; public FormFile getFile() { return file; } public void setFile(FormFile file) { this.file = file; } }