package com.liduxx.uploadfile.web;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
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.IOUtils;
@MultipartConfig
public class AjaxFilesUploadServlet extends HttpServlet
{
private static final long serialVersionUID = 513258132164002370L;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
String fileStr = new String(request.getParameter("filename")
.getBytes("ISO-8859-1"), "utf-8"); //中文乱码
System.out.println("文件名称:"+fileStr);
String files[] = fileStr.split(","); //多个文件
//获取文件需要上传到的路径
String path = request.getRealPath("/upload") + File.separator;
JSONObject json = new JSONObject();
json.put("succ", true);
JSONObject jo = new JSONObject();
for (int i = 0; i < files.length; i++)
{
String[] file = files[i].split("\\!");
String id = file[0];
String fileRef = file[1];
String name = fileRef.substring(0, fileRef.lastIndexOf("."));
String type = fileRef.substring(fileRef.lastIndexOf(".") + 1);
long size = getFileSize(path + name + "." + type);
jo.put(id, size);
json.put("data", jo);
}
System.out.println("get返回json数据:" + json);
response.setContentType("text/plain");
response.getWriter().write(json.toString());
}
/** <一句话功能简述>
* 根据文件路径取得文件大小
* @param filePath 文件路径
* @return 返回文件大小
*
* @return long [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
private long getFileSize(String filePath)
{
long size = 0;
File file = new File(filePath);
if (file.exists())
{
size = file.length();
}
return size;
}
/**
* 上传流文件并保存
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
request.setCharacterEncoding("utf-8"); //设置编码
System.out.println("开始访问servlet(dopost) :");
JSONObject json = new JSONObject(); //返回的json串
String filename = ""; //文件名称
String path = request.getRealPath("/upload"); //获取文件需要上传到的路径
try
{
List<FileItem> items = new ServletFileUpload(
new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items)
{
if (item.isFormField())
{
// Process regular form field (input type="text|radio|checkbox|etc", select, etc).
String fieldname = item.getFieldName();
String fieldvalue = "";
if (fieldname.equals("name"))
{
filename = fieldvalue = URLDecoder.decode(item.getString(),
"UTF-8");
}
else
{
fieldvalue = item.getString();
}
System.out.println("fieldname:" + fieldname
+ "--fieldvalue:" + fieldvalue);
// ... (do your job here)
}
else
{
// Process form file field (input type="file").
String fieldname = item.getFieldName();
//String filename = FilenameUtils.getName(item.getName());
InputStream filecontent = item.getInputStream();
System.out.println("fieldname:" + fieldname + "--filename:"
+ filename + "---filecontent:" + filecontent
+ "---path:" + path);
// item.write( new File(path,filename) );//第三方提供的;
//手动写入硬盘
if (makeDir(path))
{
createFile(path, filename);
}
File file = new File(path + File.separator + filename);
FileOutputStream fos = new FileOutputStream(file, true);
InputStream is = item.getInputStream();
IOUtils.copy(is, fos);
is.close();
fos.close();
System.out.println("获取上传文件的总共的容量:" + item.getSize());
}
}
}
catch (FileUploadException e)
{
e.printStackTrace();
}
/** 前端参数 **/
// String filenameaaa = request.getParameter("name");
// String fileid = request.getParameter("fileid");
// String file = request.getParameter("file");
// String bytestart = request.getParameter("start");
// //获得磁盘文件条目工厂
// DiskFileItemFactory factory = new DiskFileItemFactory();
// //获取文件需要上传到的路径
// String path = request.getRealPath("/upload");
// //创建目录
// makeDir(path);
// //如果没以下两行设置的话,上传大的 文件 会占用 很多内存,
// //设置暂时存放的 存储室 , 这个存储室,可以和 最终存储文件 的目录不同
// /**
// * 原理 它是先存到 暂时存储室,然后在真正写到 对应目录的硬盘上,
// * 按理来说 当上传一个文件时,其实是上传了两份,第一个是以 .tem 格式的
// * 然后再将其真正写到 对应目录的硬盘上
// */
// factory.setRepository(new File(path));
// //设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到 暂时存储室
// factory.setSizeThreshold(1024*1024) ;
//
// //高水平的API文件上传处理
// ServletFileUpload upload = new ServletFileUpload(factory);
//
//
// try {
// //可以上传多个文件
// List<FileItem> list = (List<FileItem>)upload.parseRequest(request);
//
// for(FileItem item : list)
// {
// //获取表单的属性名字
// String name = item.getFieldName();
//
// //如果获取的 表单信息是普通的 文本 信息
// if(item.isFormField())
// {
// //获取用户具体输入的字符串 ,�
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
uploadfile20170503初级基础版.zip (37个子文件)
uploadfile
test
com
iman
EmbeddedTomcat.java 2KB
resources
log4j.properties 304B
.settings
org.eclipse.core.resources.prefs 57B
org.eclipse.jst.j2ee.ejb.annotations.xdoclet.prefs 149B
src
com
liduxx
uploadfile
web
AjaxFilesUploadServlet.java 11KB
.project 1KB
WebRoot
css
hl.css 2KB
btn.css 8KB
demo.css 2KB
upload
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 661B
classes
log4j.properties 304B
com
iman
EmbeddedTomcat.class 3KB
liduxx
uploadfile
web
AjaxFilesUploadServlet.class 6KB
html5Test.html 1KB
index.html 21KB
work
Tomcat
localhost
uploadfile
org
apache
jsp
html5Test_jsp.java 5KB
js
jquery-1.11.3.min.js 94KB
index2.html 8KB
inde3.html 1KB
index备份.html 20KB
.classpath 2KB
lib
log
slf4j-api-1.6.1.jar 25KB
json
ezmorph-1.0.6.jar 84KB
json-lib-2.4-jdk15.jar 155KB
embed7-lib
tomcat-embed-core-7.0.26.jar 2.37MB
servlet-api.jar 173KB
tomcat-dbcp-7.0.26.jar 230KB
tomcat-embed-logging-juli-7.0.26.jar 37KB
tomcat-embed-jasper-7.0.26.jar 769KB
ecj-3.7.1.jar 1.67MB
commons
commons-logging-1.0.4.jar 37KB
commons-io-2.0.1.jar 156KB
commons-beanutils-1.8.2.jar 226KB
commons-fileupload-1.2.2.jar 58KB
commons-collections-3.2.jar 558KB
commons-lang-2.5.jar 273KB
共 37 条
- 1
资源评论
lvyingkai1
- 粉丝: 0
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功