uploadddxc.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 293 行

JAVA
293
字号
package com.redmoon.kit.util;import java.io.*;import java.util.*;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import cn.js.fan.util.ErrMsgException;import cn.js.fan.util.file.FileUtil;import org.apache.log4j.Logger;import cn.js.fan.web.Global;public class UploadDdxc {    public Logger logger = Logger.getLogger(UploadDdxc.class.getName());    public long maxFileSize = 1024 * 600;          static Map uploadFileInfos = Collections.synchronizedMap(new HashMap());        final String upload_type_file_header = "TYPE_FILE_HEADER";    final String upload_type_thread_header = "TYPE_THREAD_HEADER";    final String upload_type_segment = "TYPE_SEGMENT";    final String upload_type_file_finished = "TYPE_FILE_FINISHED";    final String thread_segment_ok = "Thread segment ok";    final String thread_header_ok = "Thread header ok";        public final String file_finished_ok = "File finished ok";    public final String file_header_ok = "File header ok";    final String file_count_exceed_max = "Too many files are being uploaded. Please wait.";    public String visualPath = "";     public UploadDdxc() {        UploadReaper.initInstance(UploadReaper.getReapInterval());    }    public Map getUploadFileInfos() {        return uploadFileInfos;    }    public String receive(ServletContext application,                          HttpServletRequest request) throws ErrMsgException {        FileUpload fu = doUpload(application, request);        return op(request, fu);    }    public FileUpload doUpload(ServletContext application,                               HttpServletRequest request) throws            ErrMsgException {        FileUpload fu = new FileUpload();        fu.setMaxFileSize((int) maxFileSize);                         int ret = 0;        try {            ret = fu.doUpload(application, request);            if (ret == fu.RET_TOOLARGESINGLE) {                throw new ErrMsgException(fu.getErrMessage());            }            if (ret == fu.RET_INVALIDEXT) {                throw new ErrMsgException(fu.getErrMessage());            }        } catch (IOException e) {            logger.error("doUpload:" + e.getMessage());        }        return fu;    }    public String op(HttpServletRequest request, FileUpload fu) throws ErrMsgException {        String uploadType = fu.getFieldValue("uploadType");        if (uploadType == null) {            logger.error("want uploadType");            Enumeration enu = fu.getFields();            while (enu.hasMoreElements()) {                String key = (String)enu.nextElement();                logger.info("op: key=" + key + " value=" + fu.fields.get(key));            }            return "want uploadType";        }        String re = "";        if (uploadType.equals(upload_type_file_header)) {            re = ReceiveUploadFileHeader(request, fu);        } else if (uploadType.equals(upload_type_thread_header)) {            re = ReceiveUploadThreadHeader(fu);        } else if (uploadType.equals(upload_type_segment)) {            re = ReceiveUploadThreadFileSegment(fu);        } else if (uploadType.equals(upload_type_file_finished)) {            re = ReceiveUploadFileFinished(request, fu);        }        else            throw new ErrMsgException("Upload type error.");        return re;    }    public void setMaxFileSize(long maxFileSize) {        this.maxFileSize = maxFileSize;    }    public void setDebug(boolean debug) {        this.debug = debug;    }    public long getMaxFileSize() {        return maxFileSize;    }    public boolean isDebug() {        return debug;    }        public String ReceiveUploadFileHeader(HttpServletRequest request, FileUpload fu) throws ErrMsgException {                        if (uploadFileInfos.size()>=Global.maxUploadingFileCount)            return file_count_exceed_max;                String fileId = fu.getFieldValue("fileId");        if (fileId==null)            throw new ErrMsgException("want fileId");        String clientFilePath = fu.getFieldValue("clientFilePath");                String filepath = visualPath + "/" + fu.getFieldValue("filepath");        UploadFileInfo ufi = new UploadFileInfo();        ufi.setFileId(fileId);        ufi.setClientFilePath(clientFilePath);        ufi.setFilePath(filepath);         uploadFileInfos.put(fileId, ufi);                return file_header_ok;    }    public String ReceiveUploadThreadHeader(FileUpload fu) throws            ErrMsgException {                String fileId = fu.getFieldValue("fileId");        if (fileId == null)            throw new ErrMsgException("want fileId");        String sBlockId = fu.getFieldValue("blockId");        if (sBlockId == null)            throw new ErrMsgException("want blockId");        int blockId = Integer.parseInt(sBlockId);        UploadThreadInfo uti = new UploadThreadInfo();        uti.setFileId(fileId);        uti.setBlockId(blockId);                UploadFileInfo ufi = (UploadFileInfo) uploadFileInfos.get(fileId);        if (ufi == null) {            logger.error("ReceiveUploadThreadHeader fileId=" + fileId);            throw new ErrMsgException(                    "There are no file header for this thread.");        }        ufi.addUploadThreadInfo(uti);        try {                        String blockPath = fu.getTmpPath() + ufi.getBlockName(blockId);            File file = new File(blockPath);            if (!(file.exists()))                file.createNewFile();         } catch (IOException e) {            logger.error("创建块文件失败!" + e.getMessage());        }        return thread_header_ok;    }    public String ReceiveUploadThreadFileSegment(FileUpload fu) throws            ErrMsgException {        String fileId = fu.getFieldValue("fileId");        if (fileId == null)            throw new ErrMsgException("want fileId");                String sBlockId = fu.getFieldValue("blockId");        if (sBlockId == null)            throw new ErrMsgException("want blockId!");        String sOffset = fu.getFieldValue("offset");        if (sOffset == null)            throw new ErrMsgException("want offset!");        String sLength = fu.getFieldValue("length");        if (sLength == null)            throw new ErrMsgException("want length!");        int blockId = Integer.parseInt(sBlockId);        int offset = Integer.parseInt(sOffset);        int length = Integer.parseInt(sLength);        Vector v = fu.getFiles();        if (v.size() > 0) {            FileInfo fi = (FileInfo) v.get(0);            String tmpPath = fi.getTmpFilePath();            File file = new File(tmpPath);                        if (length != file.length()) {                logger.error("segment length=" + length + " tmp file length=" + file.length());                throw new ErrMsgException("segment error");            }                        UploadFileInfo ufi = (UploadFileInfo) uploadFileInfos.get(fileId);            if (ufi == null)                throw new ErrMsgException("want thread header");            String blockPath = fu.getTmpPath() + ufi.getBlockName(blockId);            try {                RandomAccessFile rf = new RandomAccessFile(blockPath, "rw");                                                                                rf.seek(offset);                File tmpFile = new File(fi.getTmpFilePath());                                FileInputStream fis = new FileInputStream(tmpFile);                BufferedInputStream bis = new BufferedInputStream(fis);                 byte[] buf = new byte[1024];                int len = 0;                while ((len = bis.read(buf)) != -1) {                    rf.write(buf, 0, len);                }                bis.close();                fis.close();                 rf.close();                  tmpFile.delete();             } catch (IOException e) {                logger.error("ReceiveUploadThreadFileSegment: " + e.getMessage());            }        } else            throw new ErrMsgException("want file");        return thread_segment_ok;    }    public String ReceiveUploadFileFinished(HttpServletRequest request, FileUpload fu) throws            ErrMsgException {                String fileId = fu.getFieldValue("fileId");        if (fileId == null)            throw new ErrMsgException("want fileId");        UploadFileInfo ufi = (UploadFileInfo) uploadFileInfos.get(fileId);                if (ufi == null)                throw new ErrMsgException("want thread header");                String fullSavePath = ufi.getFullSavePath(fu.getRealPath());        try {                        File file = new File(fullSavePath);                        if (!(file.exists()))                file.createNewFile();         } catch (IOException e) {            logger.error("ReceiveUploadFileFinished:" + e.getMessage());        }        ufi.setState(ufi.state_finished);        Vector v = ufi.getUploadThreadInfos();        int size = v.size();                for (int i = 0; i < size; i++) {            UploadThreadInfo uti = (UploadThreadInfo) v.get(i);            String blockPath = fu.getTmpPath() +                               ufi.getBlockName(uti.getBlockId());            if (debug) {                logger.info(uti.getFileId() + " begin=" + uti.getBegin() +                            " end=" + uti.getEnd());                logger.info("fullSavePath=" + fullSavePath);                logger.info("blockPath=" + blockPath);            }            FileUtil.AppendFile(fullSavePath, blockPath);                        File file = new File(blockPath);            file.delete();        }                uploadFileInfos.remove(fileId);        return file_finished_ok;    }    private boolean debug = true;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?