package com.hongfei.servlet;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileUpLoadServelt extends HttpServlet {
private static final long serialVersionUID = 1L;
public FileUpLoadServelt() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("1 进入 FileUpLoadServelt doPost() ");
String filename = "";
String type="";// 文件类型
String fileTempPath = System.currentTimeMillis()+""; //防止文件冲突用毫秒数作为文件夹
String savaPath = request.getParameter("savaPath"); //不同类型的文件存放在不同的目录下
savaPath = savaPath==null?"defaultUploadPath":savaPath;
//文件路径形如 X:/apache-tomcat/webapps/bsp-web/uploadFiles/images/8788888888/my.img
String filepath = this.getServletContext().getRealPath("")
+ java.io.File.separator + "uploadFiles"
+ java.io.File.separator + savaPath + java.io.File.separator
+ fileTempPath;
File filepathDir = new File(filepath);
if (!filepathDir.exists()) {
filepathDir.mkdirs();
}
System.out.println("2 filepath= " + filepath + " == ");
ServletInputStream in = request.getInputStream();
byte[] buf = new byte[4048];
int len = in.readLine(buf, 0, buf.length);
String f = new String(buf, 0, len - 1);
while ((len = in.readLine(buf, 0, buf.length)) != -1) {
filename = new String(buf, 0, len);
int j = filename.lastIndexOf("\"");
int p = filename.lastIndexOf(".");
type=filename.substring(p,j);
filename = System.currentTimeMillis()+type;
System.out.println("3 file path ="+filepath+java.io.File.separator+filename);
DataOutputStream fileStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filepath+java.io.File.separator+filename)));
len = in.readLine(buf, 0, buf.length);
len = in.readLine(buf, 0, buf.length);
while ((len = in.readLine(buf, 0, buf.length)) != -1) {
String tempf = new String(buf, 0, len - 1);
if (tempf.equals(f) || tempf.equals(f + "--")) {
break;
}
else{
fileStream.write(buf, 0, len);
}
}
fileStream.close();
}
PrintWriter out=response.getWriter();
// 返回的结果
String result = savaPath+java.io.File.separator+fileTempPath+java.io.File.separator+filename ;
System.out.println("4 result ="+result);
out.print("<pre>"+result+"</pre>");
out.close();
in.close();
}
}
jquery文件上传(真正的ajax方式)
4星 · 超过85%的资源 需积分: 44 11 浏览量
2013-04-06
23:42:20
上传
评论
收藏 79KB RAR 举报
wangmingshan
- 粉丝: 0
- 资源: 22
最新资源
- xyctf:从入门到精通的实用指南.zip
- mmqrcode1714153659780.png
- Screenshot_2024-04-27-06-08-58-486_com.baidu.xin.aiqicha.jpg
- 基于Javaweb+Tomcat+MySQL的大学生公寓管理系统+sql文件.zip
- 实训作业基于javaweb的订单管理系统源码+数据库+实训报告.zip
- 多机调度问题贪心算法基于最小堆和贪心算法求解多机调度问题.zip
- 基于同态加密技术的匿名电子投票系统源码.zip
- Pyqt5项目框架-PyQt项目开发实践
- 基于C通过MQTT的智能农业大棚管理系统(本科毕业设计)
- python+CNN的网络入侵检测算法源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
前往页