jsp+hibernate把图片路径存入数据库,怎样实现
1个回答
展开全部
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.upload.FormFile;
public class FileUpload {
// 限定图片大小
private static final int MSX_FILE_SIZE = 1024 * 1024;
// 限定图片格式
private static final String NOT_ALLOW = "notallow";
// 判断图片大小
private static final String TOO_LARGE = "toolarge";
// 图片不能为空
private static final String NOT_NULL = "notnull";
//上传图片
@SuppressWarnings("deprecation")
public static String uploadFile(FormFile formFile,
HttpServletRequest request) {
InputStream is = null;
OutputStream os = null;
String relatePath = null;
if (formFile != null) {
String fileName = formFile.getFileName();
String ext = getExt(fileName);
Boolean flag = isAllType(ext);
if (flag) {
int fileSize = formFile.getFileSize();
if (fileSize < MSX_FILE_SIZE) {
if (fileSize > 0) {
try {
is = formFile.getInputStream();
// 构建文件存放的路径
String path = request.getRealPath("/uploadImage");
SimpleDateFormat sdf = new SimpleDateFormat("dd");
String date = sdf.format(new Date());
// 大图片
String realPath = path + "/" + date;
fileName = System.currentTimeMillis() + "." + ext;
File parent = new File(realPath);
if (!parent.exists()) {
parent.mkdirs();
}
// 将文件写入服务器
File file = new File(parent, fileName);
os = new FileOutputStream(file);
byte[] buf = new byte[1024];
int byteRead = 0;
while ((byteRead = is.read(buf)) != -1) {
os.write(buf, 0, byteRead);
}
relatePath = "uploadImage" + "/" + date + "/" + fileName;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
return NOT_NULL;
}
} else {
return TOO_LARGE;
}
} else {
return NOT_ALLOW;
}
}
return relatePath;
}
/**
* 删除指定的文件
*
* @param filePath
* 文件路径
*/
public static boolean deleteFile(String filePath) {
boolean flag = false;
if (filePath != null) {
File file = new File(filePath);
if (file.canRead() && file.isFile() && file.exists()) {
file.delete();
flag = true;
}
}
return flag;
}
/*
* 判断文件类型
*/
private static boolean isAllType(String ext) {
List<String> exts = new ArrayList<String>();
exts.add("gif");
exts.add("png");
exts.add("jpg");
exts.add("bmp");
exts.add("jpeg");
if (ext != null) {
ext = ext.toLowerCase();
if (exts.contains(ext) || "".equals(ext)) {
return true;
}
}
return false;
}
/*
* 获取文件扩展名
*/
private static String getExt(String fileName) {
if (fileName != null) {
return fileName.substring(fileName.lastIndexOf(".") + 1)
.toLowerCase();
}
return "";
}
}
或许对你有用,,
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.upload.FormFile;
public class FileUpload {
// 限定图片大小
private static final int MSX_FILE_SIZE = 1024 * 1024;
// 限定图片格式
private static final String NOT_ALLOW = "notallow";
// 判断图片大小
private static final String TOO_LARGE = "toolarge";
// 图片不能为空
private static final String NOT_NULL = "notnull";
//上传图片
@SuppressWarnings("deprecation")
public static String uploadFile(FormFile formFile,
HttpServletRequest request) {
InputStream is = null;
OutputStream os = null;
String relatePath = null;
if (formFile != null) {
String fileName = formFile.getFileName();
String ext = getExt(fileName);
Boolean flag = isAllType(ext);
if (flag) {
int fileSize = formFile.getFileSize();
if (fileSize < MSX_FILE_SIZE) {
if (fileSize > 0) {
try {
is = formFile.getInputStream();
// 构建文件存放的路径
String path = request.getRealPath("/uploadImage");
SimpleDateFormat sdf = new SimpleDateFormat("dd");
String date = sdf.format(new Date());
// 大图片
String realPath = path + "/" + date;
fileName = System.currentTimeMillis() + "." + ext;
File parent = new File(realPath);
if (!parent.exists()) {
parent.mkdirs();
}
// 将文件写入服务器
File file = new File(parent, fileName);
os = new FileOutputStream(file);
byte[] buf = new byte[1024];
int byteRead = 0;
while ((byteRead = is.read(buf)) != -1) {
os.write(buf, 0, byteRead);
}
relatePath = "uploadImage" + "/" + date + "/" + fileName;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
return NOT_NULL;
}
} else {
return TOO_LARGE;
}
} else {
return NOT_ALLOW;
}
}
return relatePath;
}
/**
* 删除指定的文件
*
* @param filePath
* 文件路径
*/
public static boolean deleteFile(String filePath) {
boolean flag = false;
if (filePath != null) {
File file = new File(filePath);
if (file.canRead() && file.isFile() && file.exists()) {
file.delete();
flag = true;
}
}
return flag;
}
/*
* 判断文件类型
*/
private static boolean isAllType(String ext) {
List<String> exts = new ArrayList<String>();
exts.add("gif");
exts.add("png");
exts.add("jpg");
exts.add("bmp");
exts.add("jpeg");
if (ext != null) {
ext = ext.toLowerCase();
if (exts.contains(ext) || "".equals(ext)) {
return true;
}
}
return false;
}
/*
* 获取文件扩展名
*/
private static String getExt(String fileName) {
if (fileName != null) {
return fileName.substring(fileName.lastIndexOf(".") + 1)
.toLowerCase();
}
return "";
}
}
或许对你有用,,
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询