由于以前一直是用的smartupload,自己写了个,写的不好,还请包涵:)
/**
*@discription 一个用于上传的类,可以用于任何类型的上传。
*@package com.tools.manage
*@author 李国庆
*@company LEEMENZ
*@version 1.0.0
*
*/
package com.tools.manage;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
/**
* @author Administrator
*
*/
public class Upload {
private ServletRequest request;
private ServletResponse response;
private ServletConfig config;
private ServletInputStream DATA;
private int FormSize;
private File file;
private FileOutputStream foutput;
private DataInputStream dinput;
private String filename;
private byte[] b;
private byte t;
private boolean flag = false;
public Upload() {
}
/**
* @description 初始化上下文配置
* @param config 上下文配置
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException 抛出IO异常
*/
public void initialize(ServletConfig config, HttpServletRequest request,
HttpServletResponse response) throws IOException {
this.request = request;
this.config = config;
this.response = response;
this.DATA = request.getInputStream();
this.FormSize = request.getContentLength();
}
/**
* @description 初始化上下文
* @param pageContext 上下文文件
* @throws IOException 抛出IO异常
*/
public void initialize(PageContext pageContext) throws IOException {
this.request = pageContext.getRequest();
this.response = pageContext.getResponse();
this.config = pageContext.getServletConfig();
this.DATA = request.getInputStream();
this.FormSize = request.getContentLength();
}
/**
* @description 设置文件名
* @param str 传入的文件名字符串
* @return 返回boolean类型的值
*/
public boolean setFileName(String str) {
try {
File file = new File(str);
foutput = new FileOutputStream(file);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* @description 将读入的数据对象以字节的方式存放到内存中
*
*/
public void getByte() {
int i = 1;
try {
dinput = new DataInputStream(DATA);
b = new byte[FormSize];
while (true) {
try {
t = dinput.readByte();
b[i] = t;
i++;
} catch (EOFException e) {
// TODO: handle exception
break;
}
}
dinput.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* @description 以ISO8859-1标准保存文件
* @return 保存成功的标志
*/
public boolean save() {
int start1 = 0, start2 = 0;
String temp = "";
if (!flag) {
getByte();
flag = true;
}
try {
temp = new String(b, "ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO: handle exception
return false;
}
start1 = temp.indexOf("image/");
temp = temp.substring(start1);
start1 = temp.indexOf(" ");
temp = temp.substring(start1 + 4);
start2 = temp.indexOf("; ");
if (start2 != -1) {
temp = temp.substring(0, start2);
}
try {
byte[] img = temp.getBytes("ISO8859-1");
for (int j = 0; j < img.length; j++) {
foutput.write(img[j]);
}
foutput.close();
} catch (IOException e) {
// TODO: handle exception
return false;
}
return true;
}
/**
* @description 在物理存储上创建文件夹
* @param pathOld 存放物理位置
* @param pathNew 在相应的物理位置下要创建的文件夹名称或序列
* @return string 返回创建好的文件夹的物理位置绝对路径
*/
public String MAKE_DIR(String pathOld, String pathNew) {
try {
if (!(new File(pathOld).isDirectory())) {
new File(pathOld);
new File(pathOld + "/" + pathNew).mkdirs();
} else {
new File(pathOld + "/" + pathNew).mkdirs();
}
} catch (SecurityException e) {
System.out.println("can not create directory");
}
return "/" + pathNew + "/";
}
}
评论