📄 uploadbean.java
字号:
package com.cric.onlineshop.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 UploadBean {
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 UploadBean() {
}
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 + -