package com.sunny.main;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* 实现多文件的同时上传
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
// 设置接收的编码格式
request.setCharacterEncoding("UTF-8");
Date date = new Date();// 获取当前时间
SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat sdfFolder = new SimpleDateFormat("yyMM");
String newfileName = sdfFileName.format(date);// 文件名称
String fileRealPath = "";// 文件存放真实地址
String fileRealResistPath = "";// 文件存放真实相对路径
// 名称 界面编码 必须 和request 保存一致..否则乱码
// String name = request.getParameter("name");
String name = session.getAttribute("LOGINUSER").toString();
String firstFileName = "";
// 获得容器中上传文件夹所在的物理路径
String savePath = this.getServletConfig().getServletContext()
.getRealPath("/")
+ "uploads\\" + newfileName + "\\";
File file = new File(savePath);
if (!file.isDirectory()) {
file.mkdirs();
}
try {
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("UTF-8");
// 获取多个上传文件
// Map<String, List<FileItem>>
// filemap=upload.parseParameterMap(request);
List<FileItem> fileList = upload.parseRequest(request);
// 遍历上传文件写入磁盘
Iterator it = fileList.iterator();
while (it.hasNext()) {
Object obit = it.next();
if (obit instanceof DiskFileItem) {
DiskFileItem item = (DiskFileItem) obit;
// 如果item是文件上传表单域
// 获得文件名及路径
String fileName = item.getName();
if (fileName != null) {
firstFileName = item.getName().substring(
item.getName().lastIndexOf("\\") + 1);
String formatName = firstFileName
.substring(firstFileName.lastIndexOf("."));// 获取文件后缀名
fileRealPath = savePath + newfileName + formatName;// 文件存放真实地址
BufferedInputStream in = new BufferedInputStream(
item.getInputStream());// 获得文件输入流
BufferedOutputStream outStream = new BufferedOutputStream(
new FileOutputStream(new File(fileRealPath)));// 获得文件输出流
Streams.copy(in, outStream, true);// 开始把文件写到你指定的上传文件夹
// 上传成功,则插入数据库
if (new File(fileRealPath).exists()) {
// 虚拟路径赋值
fileRealResistPath = fileRealPath
.substring(fileRealPath.lastIndexOf("\\") + 1);
// 保存到数据库
String user = session.getAttribute("LOGINUSER")
.toString();
Connection cnn = null;
PreparedStatement pst = null;
try {
//将文件信息插入数据库
Class.forName("com.mysql.jdbc.Driver");
cnn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/file?user=root&password=");
String sql="insert into file(user,path,realname) values('"+user+"','"+newfileName+"','"+fileName+"')";
pst = cnn.prepareStatement(sql);
pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (pst != null) {
pst.close();
pst = null;
}
if (cnn != null) {
cnn.close();
cnn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//加入聊天信息中
String str=user+"上传了文件:"+"<a href='uploads/"+newfileName+"/"+newfileName+".zip'>"+fileName+"</a>";
HelloServlet.strSendConentList.add(str);
}
}
}
}
} catch (FileUploadException ex) {
ex.printStackTrace();
System.out.println("没有上传文件");
return;
}
response.getWriter().write("1");
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于java服务端的html聊天室
共42个文件
gif:10个
js:5个
css:4个
5星 · 超过95%的资源 需积分: 17 20 下载量 185 浏览量
2015-04-29
22:00:39
上传
评论
收藏 1.04MB RAR 举报
温馨提示
可以上传文件和进行聊天,但是因为字符串内容存储在ArrayList中所以存储有限制,并且时间上有些问题
资源推荐
资源详情
资源评论
收起资源包目录
chat.rar (42个子文件)
chat
.project 1KB
WebContent
WEB-INF
lib
mysql-connector-java-5.1.26-bin.jar 836KB
commons-io-1.3.2.jar 86KB
commons-fileupload-1.2.1.jar 56KB
web.xml 919B
ChatMain.html 3KB
Face
9.gif 3KB
4.gif 1KB
3.gif 3KB
6.gif 2KB
2.gif 2KB
7.gif 2KB
5.gif 971B
10.gif 4KB
1.gif 2KB
8.gif 5KB
img
uploadify-cancel.png 3KB
js
uploadify.css 2KB
login.js 868B
jsmain.js 4KB
uploadify.swf 12KB
jquery-1.4.2-vsdoc.js 229KB
jquery.uploadify.min.js 45KB
jquery-1.8.0.min.js 90KB
Login.html 859B
META-INF
MANIFEST.MF 39B
css
uploadify.css 2KB
main.css 1KB
login.css 872B
src
com
sunny
main
UploadServlet.java 5KB
HelloServlet.java 5KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.ws.service.policy.prefs 87B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.core.resources.prefs 85B
org.eclipse.wst.common.component 464B
.jsdtscope 503B
build
classes
com
sunny
main
HelloServlet.class 5KB
UploadServlet.class 7KB
.classpath 841B
共 42 条
- 1
资源评论
- foolere2015-05-08正在学习,有些帮助。谢谢
课本小明
- 粉丝: 58
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- DatabaseWatermarkingBasedonTextFormat
- -移动通信-网络课程设计与研究
- 基于Python的PCA人脸识别算法的原理及实现代码+文档详解.zip
- 甘肃移动全业务工程建设项目管理流程优化研究
- 549springboot + vue 民宿管理平台.zip (可运行源码+数据库文件+文档)
- ZArchiver.Pro_0.9.5.apk
- vmware环境配置.mp4
- 548springboot + vue 大学生社团活动平台.zip(可运行源码+数据库文件+文档)
- 微信小程序 辩论倒计时小程序源码 作业设计demo 计算机专业参考
- 深入探究文件IO,嵌入式Linux
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功