?? uploadservice.java
字號:
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]; //讀入流緩沖區(qū)
private byte writeByte[] = new byte[4096]; //寫入流緩沖區(qū)
private int readCount[] = new int[1]; //readCount[]為行字符數(shù)
private int writeCount[] = new int[1]; //設置指針變量
/**
* 配置參數(shù)
*/
public void setFileParam(FileParam fileParam) {
this.fileParam = fileParam;
}
/**
* 有外部設置供內(nèi)部調(diào)用(文件和文件夾)
*/
/*
* 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) {
//定義數(shù)據(jù)塊起始段標志
this.boundary ="--" + strBoundary.substring(boundaryLine + 9);
//System.out.println("boundary = " + boundary);
}
}
/**
* readLine(byte[] buffer, int offset, int length)
*
* @return 從輸入流中讀入行數(shù)據(jù)
*/
private String readLine(byte readByte[], int readCount[],
ServletInputStream servletInputStream, String CharacterEncoding) {
try {
readCount[0] = servletInputStream.readLine(readByte, 0,
readByte.length);
//如果讀入的長度為 -1 ,即輸入流數(shù)據(jù)已讀完
if (readCount[0] == -1) {
return null;
}
}
catch (IOException ex) {
return null;
}
try {
if (CharacterEncoding == null) {
/**
* 使用缺省得編碼轉換字符串
*/
return new String(readByte, 0, readCount[0]);
}
else {
/**
* 由指定的編碼方式把給定的byte數(shù)組轉換為字符串
*/
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) {
// 統(tǒng)一處理 : 防止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(" 該系統(tǒng)不支持您上傳的文件類型 ");
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);
}
/**
* 創(chuàng)建文件
*/
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);
// 根據(jù)目錄參數(shù),創(chuàng)建無限層的目錄結構
FileUtil.makeDir(middleDir,context);
}
else{
tempFileDir = context;
}
//文件判斷
String tempFileName = fileParam.getFileName();
if(tempFileName == null){
// 上傳后的文件名不改變
buildFilePath = middleDir + fileName + "." + fileSuffix;
}
else{
buildFilePath = middleDir + tempFileName + "." + fileSuffix;
}
// 創(chuàng)建文件
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; //文件個數(shù)
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 + -