⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadservice.java

📁 Ntsky文件上传组件! 是别人写好了的。直接用就可以了!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ntsky.file;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.ntsky.file.FileParam;

//import com.ntsky.util.CodeFilter;
/**
 * Title: NtSky 文件上传通用组件 Description: 文件上传 Copyright: Copyright (c) 2004
 * Company: www.ntsky.com
 * 
 * @author 姚君林
 * @version 1.0
 */

public class UploadService implements Upload {

    private String CharacterEncoding = "UTF-8";

    private String boundary = "";

    private FileParam fileParam = null;

    private String tempFileDir = null;
    
    private String fileName = null;

    private String buildFilePath = null;

    private String contextPath = null;

    private Map map = new HashMap();

    private byte readByte[] = new byte[4096]; //读入流缓冲区

    private byte writeByte[] = new byte[4096]; //写入流缓冲区

    private int readCount[] = new int[1]; //readCount[]为行字符数

    private int writeCount[] = new int[1]; //设置指针变量

    /**
     * 配置参数
     */
    public void setFileParam(FileParam fileParam) {
        this.fileParam = fileParam;
    }

    /**
     * 有外部设置供内部调用(文件和文件夹)
     */
    /*
     * public void setFileDir(String fileDir){ this.fileDir = fileDir;
     * isOutSideDir = true; } public void setFileName(String fileName){
     * this.fileName = fileName; isOutSideFile = true; }
     */

    /**
     * 上传到服务器的路径
     * 
     * @param contextPath
     *            String
     */
    public void setContextPath(String contextPath) {
        this.contextPath = contextPath;
    }

    /**
     * 入库时得到的最终上传后的文件路径
     */
    public String getBuildFilePath() {
        return this.buildFilePath;
    }

    /**
     * 设置字符编码方式
     */
    private void setCharacterEncoding(String characterEncoding) {
        CharacterEncoding = characterEncoding;
    }

    /**
     * 获得流格式字符
     */
    private void setBoundary(String strBoundary) {
        int boundaryLine = -1;
        //判断有无boundary来判别是否为文件上传
        if ((boundaryLine = strBoundary.indexOf("boundary=")) != -1) {
            //定义数据块起始段标志
            this.boundary ="--" + strBoundary.substring(boundaryLine + 9);
            //System.out.println("boundary = " + boundary);
        }
    }

    /**
     * readLine(byte[] buffer, int offset, int length)
     * 
     * @return 从输入流中读入行数据
     */
    private String readLine(byte readByte[], int readCount[],
            ServletInputStream servletInputStream, String CharacterEncoding) {
        try {
            readCount[0] = servletInputStream.readLine(readByte, 0,
                    readByte.length);
            //如果读入的长度为 -1 ,即输入流数据已读完
            if (readCount[0] == -1) {
                return null;
            }
        }
        catch (IOException ex) {
            return null;
        }
        try {
            if (CharacterEncoding == null) {
                /**
                 * 使用缺省得编码转换字符串
                 */
                return new String(readByte, 0, readCount[0]);
            }
            else {
                /**
                 * 由指定的编码方式把给定的byte数组转换为字符串
                 */
                return new String(readByte, 0, readCount[0], CharacterEncoding);
            }
        }
        catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取子串的名字"name="后面的值
     */
    private String getName(String line) {
        //截取"name="字符串后面的子串
        String name = line.substring(line.indexOf("name=") + 6,
                line.length() - 1);
        //截取 "name=" 后面的文本框的名称
        name = name.substring(0, name.lastIndexOf("filename=") - 3);
        //System.out.println("文本框的名称 : " + name);
        return name;
    }

    /**
     * 取得filename最后的文件名
     */
    private String getFilename(String filename) {

        // 统一处理 : 防止windows和linux的情况不兼容,全部转化为小写
        filename = filename.toLowerCase();

        //没有上传的情况
        if (filename.length() == 0) {
            return null;
        }
        //从后向前检索"\"字符
        int i = filename.lastIndexOf("\\");
        /**
         * linux情况检索"/"符号
         */
        if (i < 0 || i >= filename.length() - 1) {
            i = filename.lastIndexOf("/");
            if (i < 0 || i >= filename.length() - 1) {
                return filename;
            }
        }
        return filename.substring(i + 1);
    }

    /**
     * 文件名判断(从配置文件中取是否符合文件后缀名的要求)
     */
    private int validateFileSuffix(String fileSuffix) {
        
        //如果不设置文件类型,就表示支持全部类型
        
        // System.out.println("文件格式 : " + fileParam.getFileType());
        String fileType = fileParam.getFileType();
        if(fileType != null){
	        if (!(FileUtil.getFileType(fileType)
	                .contains(fileSuffix))) {
	            //throw new FileUploadException(" 该系统不支持您上传的文件类型 ");
	            return 1;
	        }
        }
        // System.out.println("文件格式检测完毕! ");
        return -1;
    }

    /**
     * 设置文件路径
     */
    private File setFilePath(String fileSuffix) {
        File file = null;
        String context = FileUtil.htmlEncode(contextPath);
        return createFile(file, context, fileParam.getFileDir(), fileSuffix);
    }

    /**
     * 创建文件
     */
    private File createFile(File file, String context, String fileDir,
            String fileSuffix) {
        
        String middleDir = ""; 
        if(fileDir != null){
	        middleDir = FileUtil.getAllPath(FileUtil.htmlEncode(fileDir));
	        tempFileDir = context+middleDir;
	        
	        /**
             * 目录判断
             */
	        file = new File(tempFileDir);
	        
	        // 根据目录参数,创建无限层的目录结构
	        FileUtil.makeDir(middleDir,context);     
        }
        else{
            tempFileDir = context;
        }
        
        //文件判断
        String tempFileName = fileParam.getFileName();
        if(tempFileName == null){
            // 上传后的文件名不改变
            buildFilePath = middleDir + fileName + "." + fileSuffix;
        }
        else{
            buildFilePath = middleDir + tempFileName + "." + fileSuffix;       
        }
        // 创建文件
        file = FileUtil.makeFile(context + buildFilePath);
        return file;
    }

    /**
     * 本地文件信息
     */
    private String[] localFile() {
        String[] fileInfo = new String[2];
        File file = new File(tempFileDir);
        String[] strFiles = file.list();
        int intFileCount = 0; //文件个数
        long lngSize = 0; //文件总长度
        for (int i = 0; i < strFiles.length; i++) {
            File fileTemp = new File(tempFileDir + strFiles[i]);
            //文件是否存在
            if (fileTemp.exists()) {
                //判断是否是普通文件

⌨️ 快捷键说明

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