data/attach/1904/lsk6e667g97j7q6ptqv2kwexbhsr2zcg.jpg
查阅时在网上发现的,觉得不错,转过来让自己以后有需要可以看看 顺便附上我自己的jsp页面代码
public static void showPic(String pic,HttpServletResponse response) {
try{
InputStream inputStream = null;
ServletOutputStream outputStream=response.getOutputStream();
response.reset();
response.setContentType("image/jpeg");
String savePath = "";
// 系统文件路径
String globalPath = FileUploadContext.getGlobalPath();
if (globalPath == null) {
globalPath = "D://software//";
}
savePath = globalPath+pic;
try {
inputStream = new FileInputStream(savePath);
int bufferSize = 4096;
byte[] b = new byte[bufferSize];
int n = 0;
do {
n = inputStream.read(b);
if (n > 0) {
outputStream.write(b, 0, n);
}
} while (n > 0);
outputStream.flush();
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}catch(IOException e){
logger.error(e.getMessage());
}
}
public static String uploadPic(HttpServletRequest req , String errorPic){
MultipartFile file = ((DefaultMultipartHttpServletRequest) req).getFile("pic");
String fileName = errorPic;
if(file!=null && file.getSize() > 0){
try{
if(file.getSize()>10000000){
throw new BaseException("上传失败:文件大小不能超过10M");
}
String picPath = FileUploadContext.getGlobalPath();
fileName = FileUploadContext.nextFileName();
int point = fileName.lastIndexOf(".");
fileName = fileName.substring(0,point);
String realFileName = file.getOriginalFilename();
int len = realFileName.length();
point = realFileName.lastIndexOf(".");
String prefix = realFileName.substring(point,len);
if(prefix.equals("")){
throw new IllegalArgumentException("文件无扩展名!");
}else if (prefix.equalsIgnoreCase(".jpg")
|| prefix.equalsIgnoreCase(".gif")
|| prefix.equalsIgnoreCase(".bmp")) {
// system.out.println("upload file type is " + prefix);
fileName = fileName + prefix ;
} else {
throw new IllegalArgumentException("文件类型错误!");
}
String fileFullName = picPath + fileName;
File destFile = new File(fileFullName);
file.transferTo(destFile);
}catch(Exception e){
logger.error(e.getMessage());
return fileName;
}
}
return fileName;
}
|
|
|
${st.mark} |
方式2:将图片存入数据库blob字段中(如表数据量较大不推荐使用)
public static byte[] savePic(ServletContext context, String fileName) {
byte[] buf = null;//10k缓存
try{
String picPath = context.getRealPath("WEB-INF");
int point = picPath.lastIndexOf(File.separator);
picPath = picPath.substring(0,point);
picPath = picPath + fileName;
FileInputStream imgis = new FileInputStream(picPath);
int n = 0;
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
byte[] b = new byte[4096];
try {
while ((n = imgis.read(b)) != -1) {
out.write(b, 0, n);
}
} finally {
if (imgis != null) {
imgis.close();
}
}
buf = out.toByteArray();
}catch(IOException e){
logger.error(e.getMessage());
}
return buf;
}
public static void showPic(HttpServletResponse response,byte[] b) {
try{
ServletOutputStream outputStream=response.getOutputStream();
response.reset();
response.setContentType("image/jpeg");
try {
outputStream.write(b, 0, b.length);
outputStream.flush();
} finally {
if (outputStream != null) {
outputStream.close();
}
}
}catch(IOException e){
logger.error(e.getMessage());
}
}