问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

uploadify批量上传??

发布网友 发布时间:2022-04-12 14:27

我来回答

2个回答

热心网友 时间:2022-04-12 15:56

创建了一个web project
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar

首先把这两个jar文件引入了path
然后创建了一个servlet
Upload.java(用的MyEclipse自动配置了web.xml部署描述符)

[java] view plaincopy
//Upload.java
package com.uploadify.servlet;

import java.io.File;
import java.io.IOException;
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.uploadify.utils.IPTimeStamp;

public class Upload extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String savePath = this.getServletConfig().getServletContext()
.getRealPath("");
// 得到项目的工作目录
savePath = savePath + "/uploads/";
File f1 = new File(savePath);
// 如果没有的话就创建目录
if (!f1.exists()) {
f1.mkdirs();
}
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("utf-8");
List fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
return;
}
Iterator<FileItem> it = fileList.iterator();
String name = "";
String extName = "";
while (it.hasNext()) {
FileItem item = it.next();
if (!item.isFormField()) {

// 解析文件
name = item.getName();
long size = item.getSize();
String type = item.getContentType();
if (name == null || name.trim().equals("")) {
continue;
}
// 得到文件的扩展名
if (name.lastIndexOf(".") >= 0) {
extName = name.substring(name.lastIndexOf("."));
}
File file = null;
do {
// 利用客户端IP+时间+三位随机数字生成非重复文件名:
name = new IPTimeStamp().getIPTimeStampRandom();
file = new File(savePath + name + extName);
} while (file.exists());
File saveFile = new File(savePath + name + extName);
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
response.getWriter().print(name + extName);
}

}
生成文件名字的Utils类,可以清楚的记录名字,然后对上传的文件进行处理,并且防止文件名字重复

[java] view plaincopy
//PTimeStamp.java
package com.uploadify.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class IPTimeStamp {
private String ip;
private Date date;
private SimpleDateFormat format;

public IPTimeStamp() {
try {
ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public String getTimeStamp() {
format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return format.format(new Date());
}

public String addZero(String str, int len) {
StringBuffer sb = new StringBuffer();
sb.append(str);
while (sb.length() < len) {
sb.insert(0, "0");
}
return sb.toString();
}

public String getIPTimeStampRandom() {
StringBuffer sb = new StringBuffer();
String[] ips = this.ip.split("\\.");

for (int j = 0; j < ips.length; j++) {
// System.out.println(ips[j]);
sb.append(this.addZero(ips[j], 3));
}
sb.append(this.getTimeStamp());
Random rod = new Random();
for (int i = 0; i < 3; i++) {
sb.append(rod.nextInt(10));
}
return sb.toString();
}
}

部署描述符

[html] view plaincopy
<!--web.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Upload</servlet-name>
<servlet-class>com.uploadify.servlet.Upload</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Upload</servlet-name>
<url-pattern>/Upload</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

在WebROOT下面创建folder->js

放入解压包里面的
jquery.uploadify.v2.1.4.js
jquery.uploadify.v2.1.4.min.js
jquery-1.4.2.min.js
swfobject.js

css->
uploadify.css

images->
cancel.png
[html] view plaincopy
<!--index.jsp-->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link href="css/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript" src="js/jquery.uploadify.v2.1.4.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : 'uploadify.swf',
'script' : 'Upload',
'cancelImg' : 'images/cancel.png',
'folder' : 'uploads',
'queueID' : 'fileQueue',
'auto' : false,
'multi' : true,
'simUploadLimit' : 2,
'buttonText' : 'BROWSE'
});
});
</script>
</head>

<body>
<div id="fileQueue"></div>
<input type="file" name="uploadify" id="uploadify" />
<p>
<a href="javascript:jQuery('#uploadify').uploadifyUpload()">开始上传</a>
<a href="javascript:jQuery('#uploadify').uploadifyClearQueue()">取消所有上传</a>
</p>
</body>
</html>

热心网友 时间:2022-04-12 17:14

点点滴滴点点滴滴点点滴滴点点滴滴、
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
年终会活动策划方案 深度解析:第一财经回放,探索财经新风向 逆水寒手游庄园怎么邀请好友同住 逆水寒手游 逆水寒不同区可以一起组队吗? 逆水寒手游 逆水寒怎么进入好友世界? 逆水寒手游 逆水寒怎么去别人的庄园? 使用puppeteer实现将htmll转成pdf 内卷时代下的前端技术-使用JavaScript在浏览器中生成PDF文档 【译】将HTML转为PDF的几种实现方案 变形金刚08动画怎么样 弘缠院缠豆怎么赚 52投资缠论李晓军用什么炒股软件 缠中说禅逐句讲解,的缠论中是什么编写软件? 请问缠中说禅用的飞狐分析师软件哪里下载,是要收费的软件吗? 用缠论炒股的软件 对缠论支持最好的股票软件是什么? 缠师用什么软件看盘? 染抖音蓝发色可以拍婚纱照吗 缠论视频用什么软件制作的 如何评价抖音呗呗兔? 如何评价抖音网红井川里予?,, 深圳招商银行总部的地址 招商银行总部 抖音davies是谁? 真丝连衣裙粘身上应该怎么处理? php表单调用uploadify上传文件后,提交表单时怎么获取上传的文件名? 高颜值的杯壁草莓慕斯怎么做? 草莓慕斯怎么做 ,草莓慕斯做出来不好吃怎么办?没有味道啊,就看着漂亮。。 慕斯是什么啊 手机无边框设计有哪些缺点? 无边框手机有哪些 关于难忘的校园生活的作文 大神们,手机屏幕要做到无边框很难吗 无边框手机能否成为未来手机新主流? 真正的无边框手机是什么牌子? 校园生活一件事 作文 550字左右 最新无边框手机哪款好 tomofree电动车是杂牌么 什么是无边框手机 tomofree是德国品牌吗 tomofree属于啥档次 tomofree电动车中文是什么 TOMOFREE电动车限速怎么解 手机有无边框的必要吗? 手中有2个*ST股票,后市如何操作啊.. 跪求asp uploadify asp! php+uploadify文件不能上传问题,急急! 拍高速上应急车道的车怎么上传给交警 有走应急车道照片如何上报