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

📄 uploadservice.java

📁 基于struct结构的jsp
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ntsky.bbs.file;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.Logger ;

import com.ntsky.util.CodeFilter;
/**
 * <p>Title: NtSky Open Source BBS</p>
 * <p>Description: 文件上传</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: www.fm880.com</p>
 * @author 姚君林
 * @version 1.0
 */

public class UploadService implements Upload{
    private final static Logger logger = Logger.getLogger(UploadService.class);

    private String CharacterEncoding = "UTF-8";
    private String boundary = "";

    private FileConfig fileConfig = null;
    private boolean isOutSideFile = false;
    private String fileName = null;
    private String systemFileName = null;
    private boolean isOutSideDir = false;
    private String fileDir = null;
    private String dataFilePath = null;
    private String contextPath = null;
    private List list = new ArrayList();

    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 setFileConfig(FileConfig fileConfig){
        this.fileConfig = fileConfig;
    }

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

    /**
     * 本地调用(当配置文件中路径为"-1"时使用)
     * @param characterEncoding String
     */
    public void setContextPath(String contextPath){
        this.contextPath = contextPath;
    }

    /**
     * 想入库时的路径
     */
    public String getDataFilePath(){
        return this.dataFilePath;
    }

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

    /**
     * 获得流格式字符
     */
    private void setBoundary(String boundary) {
        this.boundary = boundary;
        int isExist = -1;
        //判断有无boundary
        if ( (isExist = boundary.indexOf("boundary=")) != -1) {
            //定义数据块起始段标志
            boundary = boundary.substring(isExist + 9);
            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);
            //System.out.println("readCount[0] = " + readCount[0]);
            //如果读入的长度为 -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) {
        //没有上传的情况
        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){
        if(!(fileConfig.getType().contains(fileSuffix))){
            return 1;
        }
        return -1;
    }

    /**
     * 设置文件路径
     */
    private File setFilePath(String fileSuffix){
        File file = null;
        String path = null;
        /**
         * 目录设置
         */
        if (isOutSideDir == true) {
            //保存文件的路径
            if ("-1".equals(fileConfig.getPath())) {
                //调用系统context路径
                path = CodeFilter.htmlEncode(contextPath) + fileDir;
                logger.info("path = " + path);
            }
            else {
                path = getAllPath(getAllPath(fileConfig.getPath()) + fileDir);
                logger.info("得到文件全路径,不包含文件后缀名...." + path);
            }
            return createFile(file, path, fileDir, fileSuffix);
        }
        else {
            System.out.println("调用系统内部的目录.....");
            //系统配置的路径
            if ("-1".equals(fileConfig.getPath())) {
                //调用系统context路径
                path = CodeFilter.htmlEncode(contextPath);
            }
            else {
                //调用自己设置好的路径
                path = getAllPath(fileConfig.getPath());
            }
            return createFile(file, path, fileDir, fileSuffix);
        }
    }
    /**
     * 创建文件
     */
    private File createFile(File file,String dir,String fileDir,String fileSuffix){
        /**
         * 文件名
         */
        if (isOutSideFile == true) {
            System.out.println("调用系统外部设置的文件.......");
            /**
             * 外部提供了文件名(使用外部的文件名)
             */
            //目录判断
            file = new File(dir);
            if(!file.isDirectory()){
                file.mkdirs();
            }
            dataFilePath = "/" + getAllPath(fileDir) + fileName+"."+fileSuffix;
            //文件判断
            file = new File(dir, fileName + "." + fileSuffix);
            if(!file.exists()){
                try {
                    file.createNewFile();
                }
                catch (IOException ex) {
                    System.out.println("创建文件错误... "+ex.getMessage());
                }
            }
            return file;
        }
        else {
            System.out.println("调用系统内部设置的文件.......");
            file = new File(dir);
            System.out.println("file.isDirectory() = "+ file.isDirectory());
            if(!file.isDirectory()){
                file.mkdirs();
            }
            //文件判断
            dataFilePath = "/" + getAllPath(fileDir) + systemFileName +"."+fileSuffix;
            System.out.println("dataFilePath = " + dataFilePath);
            file = new File(dir, systemFileName + "." + fileSuffix);
            if(!file.exists()){
                try {
                    file.createNewFile();
                }
                catch (IOException ex) {
                    System.out.println("创建文件错误... "+ex.getMessage());
                }
            }
            return file;
        }
    }

    /**
     * 获得目录全名
     * @return String
     */
    private String getAllPath(String filePath){
        if (!filePath.endsWith("/")) {
            filePath += "/";
        }
        return filePath;
    }

    /**
     * 本地文件信息
     */

⌨️ 快捷键说明

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