?? uploadbeanutf.java
字號:
?package com.cric.onlineshopclothes.util;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* 用法說明
* @author fengjj
* setObjectPath("path")方法設置上傳文件路徑
* setSourceFile(request)方法傳遞HttpServletRequest
* isUploadFile()判斷是否上傳了文件
* getNewFileName()獲得新文件名稱存入數據庫中
* deleteFileExist(objectPath+oldfileName)刪除原有文件
*/
public class UploadBeanUTF{
boolean isUploadFile=false; //是否上傳了文件
private String sourceFileName=new String(); //原路徑+文件名(含后綴)
private String suffix = new String(); //原文件后綴名
private String objectPath = "c:\\"; //目標文件目錄
private String objectFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); //目標文件名
private String newFileName=new String(); //新文件名(含后綴),最后存入數據庫的文件名
private ServletInputStream sis = null; //輸入流
private String description = new String(); //描述狀態
private long size =10000*1024; //限制大小
private byte[] b = new byte[4096]; //字節流存放數組
private boolean successful = true;
private Hashtable fields = new Hashtable();
String sourceFile = new String(); //源文件名
public UploadBeanUTF() {
}
public static void main(String[] args) {
System.out.println("Test OK");
}
//刪除已有圖片
public void deleteFileExist(String filePath){
try{
File file=new File(filePath);
if(file.exists()){
file.delete();
System.out.println(filePath+"文件已刪除!");
}else{
System.out.println(filePath+"文件不存在,沒有刪除!");
}
}catch(Exception e){
e.printStackTrace();
}
}
//文件上傳處理程序
public void setSourceFile(HttpServletRequest request) throws IOException {
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (k = s.indexOf("filename=")) != -1) {
s = s.substring(k + 10);
//取得上傳的文件路徑+文件名
s=s.substring(0, s.indexOf("\""));
this.setSourceFileName(s);
//獲得上傳文件后綴
k=s.lastIndexOf("\\");
s=s.substring(k+1);
k = s.lastIndexOf(".");
suffix=s.substring(k+1);
//判斷是否上傳了文件
if(!"".equals(suffix)){
this.setUploadFile(true);
}
//獲得新文件名
this.setNewFileName(objectFileName+"."+suffix);
//生成新文件
transferFile(newFileName);
} else if ( (k = s.indexOf("name=")) != -1) {
// 普通表單輸入元素,獲取輸入元素名字
String fieldName = s.substring(k+6, s.length()-3);
sis.readLine(b, 0, b.length);
StringBuffer fieldValue = new StringBuffer(b.length);
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a-2,request.getCharacterEncoding());
if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
break;
} else {
fieldValue.append(s);
}
}
fields.put(fieldName, fieldValue.toString());
}
if (!successful)
break;
}
}
//上傳文件轉換
private void transferFile(String objectFileName) {
try {
File pathFile=new File(objectPath);
if(!pathFile.exists())
{
pathFile.mkdirs();
}
File newFile=new File(objectPath + objectFileName);
if(!newFile.exists())
newFile.createNewFile();
FileOutputStream out = new FileOutputStream(objectPath + objectFileName);
int a = 0;
int k = 0;
long hastransfered = 0; //標示已經傳輸的字節數
String s = "";
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (k = s.indexOf("Content-Type:")) != -1) {
break;
}
}
sis.readLine(b, 0, b.length);
while ( (a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
break;
}
out.write(b, 0, a);
hastransfered += a;
if (hastransfered >= size) {
description = "ERR: The file " + sourceFileName +
" is too large to transfer. The whole process is interrupted.";
successful = false;
break;
}
}
if (successful) {
description = "Right: The file " + sourceFileName+
" has been transfered successfully.";
}
out.close();
}
catch (IOException ioe) {
description = ioe.toString();
}
}
//設置文件保存路徑
public void setObjectPath(String objectPath) {
this.objectPath = objectPath;
}
//設置文件大小
public void setSize(long maxSize) {
this.size = maxSize;
}
//取得表單元素值
public String getFieldValue(String fieldName) {
if (fields == null || fieldName == null) {
return null;
}
return (String) fields.get(fieldName);
}
//取得目標路徑
public String getObjectPath() {
return objectPath;
}
//取得源文件名
public String getSourceFile() {
return sourceFile;
}
//取得目標文件名
public String getObjectFileName() {
return objectFileName;
}
//取得上傳狀態描述
public String getDescription() {
return description;
}
public void setObjectFileName(String objectFileName) {
this.objectFileName = objectFileName;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public String getSourceFileName() {
return sourceFileName;
}
public void setSourceFileName(String sourceFileName) {
this.sourceFileName = sourceFileName;
}
public boolean isUploadFile() {
return isUploadFile;
}
public void setUploadFile(boolean isUploadFile) {
this.isUploadFile = isUploadFile;
}
public String getNewFileName() {
return newFileName;
}
public void setNewFileName(String newFileName) {
this.newFileName = newFileName;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -