package org.sonny.filemanager.util; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.*; import cn.bizos.filemanager.util.FileProxy; import cn.bizos.filemanager.util.XMLBuilder; public class FileManager {
SmartUpload su = null; Request rq = null; File f = null; FileProxy fproxy = null; //XMLBuilder xb = null; public FileManager() { su = new SmartUpload(); fproxy = new FileProxy(); } public FileManager(ServletConfig config, HttpServletRequest request , HttpServletResponse response) { su = new SmartUpload(); fproxy = new FileProxy(); initialize(config, request, response); } public String createFileSpace(String userName){ createFileInstance(userName); f.mkdir(); return XMLBuilder.buildNormalDoc(); } public String createNewFolder(String folderName, String path){ createFileInstance(path + fproxy.getSeparate(path) + folderName); f.mkdir(); return XMLBuilder.buildNormalDoc(); } public String delete(String path){ File f = new File(fproxy.getValidPatch(path, false)); String[] list = f.list(); if(list != null) { for(String l : list){ delete(path + fproxy.getSeparate(path) + l); } } f.delete(); return XMLBuilder.buildNormalDoc(); } public String deleteFileSpace(String userName){ createFileInstance(userName); f.delete(); return XMLBuilder.buildNormalDoc(); } public String rename(String newpath, String oldpath){ try{ createFileInstance(oldpath); File rf = new File(fproxy.getValidPatch(newpath, false)); f.renameTo(rf); }catch(Exception e) { e.printStackTrace(); } return XMLBuilder.buildNormalDoc(); } public String copy(String from, String to, int circulatory) { try{ File frompath = new File(fproxy.getValidPatch(from, false)); if(frompath.isDirectory()) { File temp = new File(fproxy.getValidPatch(to, false)); if(!temp.exists()) temp.mkdirs(); if(circulatory == 0) { to = to + from.substring(from.lastIndexOf("/")); fproxy.getValidPatch(to, true); } String[] sublist = frompath.list(); for(int n = 0; n < sublist.length; n++) { copy(from + "/" + sublist[n], to + "/" + sublist[n], ++circulatory); } }else{ saveFileToDisk(frompath, to, circulatory); } }catch(Exception e) { e.printStackTrace(); } return XMLBuilder.buildNormalDoc(); } private void saveFileToDisk(File file, String saveTo, int circulatory) { try{ saveTo = fproxy.getValidPatch(saveTo, false); if(circulatory == 0) saveTo = saveTo + "/" + file.getName(); FileInputStream input = new FileInputStream(file); FileOutputStream output = new FileOutputStream(saveTo); byte[] b = new byte[1024 * 5]; int len; while((len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); }catch(Exception e) { e.printStackTrace(); } } private void log(String mgs) { System.out.println(mgs); } public String getCatalog(String path){ if(path != null) { if(!"undefined".equals(path) && !"$userId;".equals(path)) { if(!fproxy.isExist(path)) { fproxy.getValidPatch(path, true); } } } XMLBuilder xb = new XMLBuilder(); return xb.getFileSpaceXML(path); } public String getShareCatalog(String userName){ return null; } public String move(String from, String to) { System.out.println("run move ......"); createFileInstance(from); File dir = new File(fproxy.getValidPatch(to, false)); f.renameTo(new File(dir, f.getName())); return XMLBuilder.buildNormalDoc(); } public void initialize(ServletConfig config, HttpServletRequest request , HttpServletResponse response){ try { su.initialize(config, request, response); } catch (ServletException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String getSavedPath(String value) { String rpath = value; //rq = su.getRequest(); //if(rq != null) rpath = rq.getParameter("serverpath"); String fileName = getFileName(); rpath += fproxy.getSeparate(value) + fileName; return rpath; } public String getFileName() { return getFileName(0); } public String getFileName(int index) { String fileName = ""; com.jspsmart.upload.Files files = su.getFiles(); if(files != null) { com.jspsmart.upload.File file = files.getFile(index); if(file != null) fileName = file.getFileName(); } return fileName; } public String getSaveToPath() { String rpath = ""; rq = su.getRequest(); String name = rq.getParameter("name"); //System.out.println(name); if(rq != null) rpath = rq.getParameter("serverpath"); return rpath; } public InputStream getInputStream(int index) { InputStream rInputStream = null; com.jspsmart.upload.Files files = su.getFiles(); if(files != null) { com.jspsmart.upload.File file = files.getFile(index); if(file != null) { rInputStream = file.getInputStream(); } } return rInputStream; } public int upload(String saveTo) { try { su.upload(); //String filePath = getSaveToPath(); String savetopath = fproxy.getValidPatch(saveTo, true); return su.save(savetopath, su.SAVE_PHYSICAL); } catch (SmartUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ServletException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } public void downLoad(String filePath) { try { su.setContentDisposition(null); String path = fproxy.getValidPatch(filePath, false); su.downloadFile(path); } catch (SmartUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ServletException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void setShareFolder(String selectedPath, String shareUser){ } public void cancelShareFolder(String selectedPath, String shareUser){ } private void createFileInstance(String path){ String rpath = fproxy.getValidPatch(path, false); f = new File(rpath); } } |
|
来自: sonny--李永胜 > 《java代码集》