Created
July 30, 2012 07:27
-
-
Save eagleon/3205502 to your computer and use it in GitHub Desktop.
图片上传servlet,apache-common
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* 文 件 名: UploadServlet.java<br/> | |
* 开发人员: @author Left<br/> | |
* 邮 箱: <br/> | |
* 系统名称: <br/> | |
* 功能说明: <br/> | |
* 开发时间: 2012-6-18下午04:15:11<br/> | |
* 系统版本: @version 0.01<br/> | |
* 版权信息: Copyright © 2012-2012,Adzoner.com, All Rights Reserved<br/> | |
* 修改记录: [M+修改顺序号 修改时间 修改人员 修改原因]<br/> | |
* | |
* | |
*/ | |
package com.roshan.utils; | |
import java.io.File; | |
import java.io.IOException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Random; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.apache.commons.fileupload.FileItem; | |
import org.apache.commons.fileupload.FileUploadException; | |
import org.apache.commons.fileupload.disk.DiskFileItemFactory; | |
import org.apache.commons.fileupload.servlet.ServletFileUpload; | |
import org.apache.commons.io.FileUtils; | |
import org.apache.commons.lang3.RandomStringUtils; | |
import org.json.simple.JSONObject; | |
import org.springframework.web.context.ContextLoader; | |
import com.roshan.utils.img.Multimedia; | |
import com.roshan.website.bean.Pictures; | |
import com.roshan.website.dao.impl.ArticleDao; | |
import com.roshan.website.dao.impl.ImageDao; | |
import com.roshan.website.service.img.ImageService; | |
/** | |
* @author Z | |
* | |
*/ | |
public class UploadServlet extends HttpServlet { | |
private static ImageDao imageDao = (ImageDao) ContextLoader.getCurrentWebApplicationContext().getBean("imageDao"); | |
/** | |
* | |
* 函数功能∶创建文章的图片上传 <br/> | |
* 说 明: <br/> | |
* 1. 文件上传到临时目录,返回临时目录URL。<br/> | |
* 2. 创建文章提交文件URL,转移到/upload/用户ID/post/文章编号/xxxxx.png;保存绝对路径和URL | |
* 3.临时文件保存路径为:/upload/temp/日期 | |
* 开发人员: @author Left <br/> | |
* 创建时间:2012-6-18下午03:06:25 <br/> | |
* 相关参数:<br/> | |
* @param request | |
* @param response | |
* @return<br/> | |
* 修改记录∶ <br/> | |
* @throws IOException | |
* | |
*/ | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException { | |
SysLogUtils.debug("I am in...."); | |
response.setContentType("text/json; charset=UTF-8"); | |
JSONObject json = new JSONObject(); | |
String name = RandomStringUtils.randomAlphanumeric(15); | |
long maxSize = 1024 * 1024 * 5L; | |
StringBuilder savePath = new StringBuilder(request.getSession().getServletContext().getRealPath("/") + "upload"+File.separator); | |
StringBuilder saveUrl = new StringBuilder(ConfigFileLoader.getValue("CDN") + "/upload/"); | |
// ImageService is = new ImageService(); | |
/* | |
* 1. 处理文件上传,把取得的文件上传到tmp目录。 | |
* 2. 将tmp目录的文件转存到upload/20120301 | |
*/ | |
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory(); | |
fileItemFactory.setSizeThreshold((int)maxSize); // 1 MB | |
fileItemFactory.setRepository(new File(System.getProperty("user.dir"))); | |
ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory); | |
List items; | |
try { | |
items = uploadHandler.parseRequest(request); | |
Iterator itr = items.iterator(); | |
while (itr.hasNext()) { | |
FileItem item = (FileItem) itr.next(); | |
if (item.isFormField()) { | |
}else{ | |
//1.1 文件大小判断 | |
long size = item.getSize(); | |
if(size > maxSize){ | |
json.put("code", "-1"); | |
json.put("msg", "上传文件大小非法"); | |
response.getWriter().write(json.toJSONString()); | |
return; | |
} | |
//1.2 文件类型判断 | |
String upName = item.getName(); | |
String ext = upName.substring(upName.indexOf(".")+1).toUpperCase(); | |
String FILEEXTS = "gif,jpg,jpeg,png,bmp,JPG,GIF,JPEG,PNG,BMP"; | |
if(!FILEEXTS.contains(ext)){ | |
json.put("code", "-1"); | |
json.put("msg", "上传文件格式不合法"); | |
response.getWriter().write(json.toJSONString()); | |
return; | |
} | |
//1.3 生成新文件名 | |
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); | |
String newFileFloder = format.format(new Date()); | |
File x = new File(new StringBuffer(savePath).append("temp").toString()); | |
if(!x.exists()){ | |
FileUtils.forceMkdir(x); | |
} | |
File f = new File(new StringBuffer(savePath).append("temp").append(File.separator).toString(),name+"."+ext); | |
item.write(f); | |
//1.4 将新文件储存为制定格式文件,(插入数据库中) | |
savePath.append(newFileFloder+File.separatorChar); | |
saveUrl.append(newFileFloder+"/"); | |
File fileDir = new File(savePath.toString()); | |
if(!fileDir.exists()){ | |
FileUtils.forceMkdir(fileDir); | |
} | |
savePath.append(name); | |
saveUrl.append(name); | |
StringBuilder large = new StringBuilder(savePath.toString()).append("_large.").append(ext); | |
StringBuilder mid = new StringBuilder(savePath.toString()).append("_mid.").append(ext); | |
StringBuilder small = new StringBuilder(savePath.toString()).append("_small.").append(ext); | |
StringBuilder lurl = new StringBuilder(saveUrl.toString()).append("_large.").append(ext); | |
StringBuilder murl = new StringBuilder(saveUrl.toString()).append("_mid.").append(ext); | |
StringBuilder surl = new StringBuilder(saveUrl.toString()).append("_small.").append(ext); | |
int lheight = Multimedia.saveImage(f, new File(large.toString()), Constant.IMG_LARGE_WEIGHT, 0); | |
int mheight = Multimedia.saveImage(f, new File(mid.toString()), Constant.IMG_MID_WEIGHT, 0); | |
int sheight = Multimedia.saveImage(f, new File(small.toString()), Constant.IMG_SMALL_WEIGHT, 0); | |
Pictures p = new Pictures(); | |
p.setPath(murl.toString()); | |
p.setBigpath(lurl.toString()); | |
p.setHeight(mheight); | |
// is.insertImage(p); | |
// int pid = is.insertImage(p); | |
imageDao.insertImage(p); | |
json.put("code", "1"); | |
json.put("pid", ""+p.getPid()); | |
json.put("lurl", lurl.toString()); | |
json.put("murl", murl.toString()); | |
json.put("surl", surl.toString()); | |
SysLogUtils.debug("pid2:"+p.getPid()); | |
SysLogUtils.debug("json2:"+json.toJSONString()); | |
response.getWriter().write(json.toJSONString()); | |
} | |
} | |
} catch (FileUploadException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment