代码:
package com.siit.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
/**
* 图片上传公共类
*
* @author
*
*/
public class ImageLoad {
// private String uploadPath = "d:\\upload\\"; // 上传文件的目录
// private String tempPath = "d:\\upload\\tmp\\"; // 临时文件目录
// private int sizeThreshold = 4096;// 设置缓冲区大小
private static int sizeMax = 4194304;// 设置图片最大尺寸
/**
* 图片上传
*
* @param file
* @param fileName
* //新名
* @param absPath
* //文件保存路径
* @param uploadPath
* //文件上传路径
* @param request
*
* @return result //上传结果
* @throws Exception
*/
public static String imageUpload(File file, String fileName,String absPath, String uploadPath, HttpServletRequest request) {
String result = "";
SimpleDateFormat formatDate = new SimpleDateFormat("yyyyMMdd");
Date nowtime = new Date();
String formatnowtime = formatDate.format(nowtime);
try {
// 验证图片格式
boolean flag = readUploadFileType(file);
if (flag) {
if (file.length() > 0L && file.length() < sizeMax) {
// 上传附件
InputStream is = new FileInputStream(file);
// 1.文件保存路径(相对路径)
// String absPath = "/upload/"+invoiceForm.getInvoiceNumber()+this.getFileFileName().substring(this.getFileFileName().lastIndexOf('.'));
if (absPath == null || absPath.equals("")) {
absPath = fileName+ file.getName().substring(file.getName().lastIndexOf(".")).toLowerCase();
}
// 2.构建一个上传文件路径
// 应保证在根目录中有此目录的存在 如果没有,下面则上创建新的文件夹
// File root = new File(request.getRealPath("/")+ "uploadfile/images/" + formatnowtime +"/");
File root = null;
if (uploadPath != null && !uploadPath.equals("")) {
root = new File(uploadPath);
} else {
root = new File("d:/uploadfile/images/" + formatnowtime+ "/");
}
if (!root.isDirectory()) {
root.mkdir();
}
// 3.获得一个远程文件
File diskFile = new File(root.getAbsolutePath(), absPath);
// 4.构建输出流
OutputStream os = new FileOutputStream(diskFile);
// 5.能过字节写入输出流
byte[] buffer = new byte[8192];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();
result = "10";//图片上传成功
} else {
result = "11";// 图片大小不符,禁止上传
}
} else {
result = "12";// 图片类型不符,禁止上传
}
} catch (Exception e) {
}
return result;
}
/**
* 删除目标图片
*
* @param url
* 目标图片上传路径
* @param fileName
* 图片新命名名称
* @return
*/
public static boolean delImage(String uploadPath, String fileName) {
boolean flag = false;
File file = new File(uploadPath);
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files.length > 0) {
for (int i = 0; i < files.length; i++) {
if (files[i].getName().substring(0,files[i].getName().indexOf(".")).equals(fileName)) {
files[i].delete();
flag = true;
}
}
}
}
return flag;
}
/**
* 判断上传文件类型
*
* @param uploadfile
* @return
*/
public static boolean readUploadFileType(File uploadfile) {
boolean flag = false;
if (uploadfile != null && !uploadfile.equals("")) {
String ext = uploadfile.getName().substring(uploadfile.getName().lastIndexOf(".") + 1).toLowerCase();
//System.out.println(ext);
String[] suffix = { "jpg", "bmp", "jpeg", "pnd" };
for (int i = 0; i < suffix.length; i++) {
if (ext.equals(suffix[i])) {
flag = true;
}
}
}
return flag;
}
public static void main(String[] args) throws Exception {
// File file = new
// File("C:\\Documents and Settings\\wusongti\\My Documents\\My Pictures\\未命名.jpg");
String path = "d:/uploadfile/images/20130131/";
// System.out.println(imageUpload(file, System.currentTimeMillis() +
// "","", "", null));
System.out.println(delImage(path, "3"));
// System.out.println(readUploadFileType(file));
}
}