package com.jiong.uploadify;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
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 com.jenmin.dao.UserDao;
public class UploadServlet extends HttpServlet {
UserDao ud = UserDao.getInstance();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String userid = request.getParameter("userid");
if (userid != null && !userid.equals("")) {
String savePath = generateDir();
processUpload(request, response, savePath, Integer.parseInt(userid));
}
}
/*
* 上传处理方法
*/
public void processUpload(HttpServletRequest request,
HttpServletResponse response, String savePath, int userid) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 缓存大小丿12KB
factory.setSizeThreshold(524288);
// 临时文件夿
factory.setRepository(new File(savePath + "/temp"));
// 初妾化上传控仿
ServletFileUpload upload = new ServletFileUpload(factory);
// 文件大小朿ħ3MB
upload.setFileSizeMax(3145728);
upload.setHeaderEncoding("UTF-8");
List fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator<FileItem> it = fileList.iterator();
String name = "";
String filename = "";
while (it.hasNext()) {
FileItem item = it.next();
if (!item.isFormField()) {
name = item.getName();
if (name != null && !name.trim().equals("")) {
filename = generateFileName(name);
File file = new File(savePath + "/" + filename);
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%="+savePath);
try {
item.write(file);
String imgstr = this.getServletContext()
.getContextPath();
imgstr = imgstr + "/upload/" + filename;
ud.updateImgBySql(imgstr, userid);
response.getWriter().write("http://localhost:8080"+imgstr);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
/*
* 初妾化文件存储路徿
*/
private String generateDir() {
String pathString = getServletConfig().getServletContext().getRealPath(
"/upload");
String tempString = getServletConfig().getServletContext().getRealPath(
"/upload/temp");
File dirPath = new File(pathString);
File dirTemp = new File(tempString);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
if (!dirTemp.exists()) {
dirTemp.mkdirs();
}
return pathString;
}
/*
* 生成文件吿
*/
private String generateFileName(String name) {
long currentTime = System.currentTimeMillis();
int i = (int) (Math.random() * 1000D + 1.0D);
long result = currentTime + i;
String filename = String.valueOf(result) + getFileExt(name);
return filename;
}
/*
* 获取文件格式
*/
private String getFileExt(String name) {
int pos = name.lastIndexOf(".");
if (pos > 0) {
return name.substring(pos);
} else {
return name;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
myjcrop.zip (62个子文件)
myjcrop
.myhibernatedata 551B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.ltk.core.refactoring.prefs 137B
org.eclipse.core.resources.prefs 208B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 629B
src
com
jiong
uploadify
UploadServlet.java 4KB
ModifyServlet.java 3KB
jenmin
dao
UserDao.java 2KB
pojo
User.hbm.xml 817B
User.java 773B
sessionfactory
HibernateSessionFactory.java 3KB
hibernate.cfg.xml 890B
.project 2KB
WebRoot
css
default.css 177B
Jcrop.gif 329B
uploadify.css 2KB
jquery.Jcrop.css 748B
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 951B
classes
com
jiong
uploadify
UploadServlet.class 5KB
ModifyServlet.class 5KB
jenmin
dao
UserDao.class 2KB
pojo
User.hbm.xml 817B
User.class 1KB
sessionfactory
HibernateSessionFactory.class 2KB
hibernate.cfg.xml 890B
lib
log4j-1.2.14.jar 359KB
commons-collections-3.1.jar 546KB
hibernate-annotations.jar 273KB
dom4j-1.6.1.jar 307KB
ejb3-persistence.jar 49KB
cglib-2.2.jar 272KB
mysql-connector-java-5.0.3.jar 482KB
javassist-3.9.0.GA.jar 583KB
hibernate-commons-annotations.jar 65KB
commons-fileupload-1.2.1.jar 56KB
slf4j-api-1.5.8.jar 23KB
ehcache-1.2.3.jar 203KB
slf4j-log4j12-1.5.8.jar 9KB
jta-1.1.jar 13KB
hibernate-validator.jar 61KB
hibernate-entitymanager.jar 116KB
antlr-2.7.6.jar 433KB
commons-io-1.4.jar 106KB
hibernate3.jar 2.31MB
js
jquery.min.js 93KB
jquery.Jcrop.min.js 16KB
fileupload.js 3KB
jquery.uploadify.v2.1.0.min.js 11KB
swfobject.js 10KB
jquery.color.js 4KB
jQuery.UtrialAvatarCutter.js 945B
jquery.Jcrop.js 41KB
cancel.png 603B
uploadify.swf 23KB
json2.js 7KB
index.jsp 2KB
user.sql 246B
.mymetadata 294B
.classpath 2KB
.myeclipse
共 62 条
- 1
知道就是你
- 粉丝: 69
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页