package actions;import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;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;@SuppressWarnings("serial")
public class Uploadify extends HttpServlet {
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String savePath = this.getServletConfig().getServletContext()
.getRealPath("");
System.out.println(savePath);
savePath = savePath + "/uploads/";
File f1 = new File(savePath);
System.out.println(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();
System.out.println(size + " " + type);
if (name == null || name.trim().equals("")) {
continue;
}
//扩展名格式:
if (name.lastIndexOf(".") >= 0) {
extName = name.substring(name.lastIndexOf("."));
}
File file = null;
do {
//生成文件名:
name = UUID.randomUUID().toString();
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);
}
}jap
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=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></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head> <body>
<link href="js/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript" src="js/jquery.uploadify.v2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : 'js/uploadify.swf',
'script' : 'scripts/uploadify',//servlet的路径或者.jsp 这是访问servlet 'scripts/uploadif'
'method' :'GET', //如果要传参数,就必须改为GET
'cancelImg' : 'images/cancel.png',
'folder' : 'uploads', //要上传到的服务器路径,
'queueID' : 'fileQueue',
'auto' : false, //选定文件后是否自动上传,默认false
'multi' : true, //是否允许同时上传多文件,默认false
'simUploadLimit' : 1, //一次同步上传的文件数目
'sizeLimit' : 19871202, //设置单个文件大小限制,单位为byte
'queueSizeLimit' : 5, //限制在一次队列中的次数(可选定几个文件)。默认值= 999,而一次可传几个文件有 simUploadLimit属性决定。
// 'buttonImg' : 'images/upload.bmp' ,
'fileDesc' : '支持格式:war', //如果配置了以下的'fileExt'属性,那么这个属性是必须的
'fileExt' : '*.war',//允许的格式
// 'scriptData' :{'name':$('#name').val()}, // 多个参数用逗号隔开 'name':$('#name').val(),'num':$('#num').val(),'ttl':$('#ttl').val()
onComplete: function (event, queueID, fileObj, response, data) {
var value = response ;
alert(value);
alert("文件:" + fileObj.name + "上传成功");
},
onError: function(event, queueID, fileObj) {
alert("文件:" + fileObj.name + "上传失败");
},
onCancel: function(event, queueID, fileObj){
alert("取消了" + fileObj.name);
}
});
});
function uploasFile(){
//校验
// var name=document.getElementById("name").value;
// if(name.replace(/\s/g,'') == ''){
// alert("名称不能为空!");
// return false;
// }
///设置 scriptData 的参数
// $('#uploadify').uploadifySettings('scriptData',{'name':$('#name').val()});
//上传
jQuery('#uploadify').uploadifyUpload()
}
</script>
<form enctype="multipart/form-data" action="scripts/uploadify">
<table align="center">
<tr><td> <input type="hidden" id="name" name="name" value=fileObj.name ></td></tr>
<tr><td><input type="file" name="uploadify" id="uploadify" /></td>
<td> <div id="fileQueue"></div> </td></tr>
<tr>
<td></td>
<td><input type="button" value="版本升级" onclick="javascript:uploasFile()">
<input type="button" value="取消升级" onclick="javascript:jQuery('#uploadify').uploadifyClearQueue()"></td>
</tr>
</table>
</form>
</body>
</html>
这个在网上找的自己单独的建一个项目不会出问题,但是整合到我的项目上就会出现
一些问题 request=MultiPartRequestWrapper response=ResponseFacade
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;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;@SuppressWarnings("serial")
public class Uploadify extends HttpServlet {
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String savePath = this.getServletConfig().getServletContext()
.getRealPath("");
System.out.println(savePath);
savePath = savePath + "/uploads/";
File f1 = new File(savePath);
System.out.println(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();
System.out.println(size + " " + type);
if (name == null || name.trim().equals("")) {
continue;
}
//扩展名格式:
if (name.lastIndexOf(".") >= 0) {
extName = name.substring(name.lastIndexOf("."));
}
File file = null;
do {
//生成文件名:
name = UUID.randomUUID().toString();
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);
}
}jap
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=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></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head> <body>
<link href="js/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript" src="js/jquery.uploadify.v2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : 'js/uploadify.swf',
'script' : 'scripts/uploadify',//servlet的路径或者.jsp 这是访问servlet 'scripts/uploadif'
'method' :'GET', //如果要传参数,就必须改为GET
'cancelImg' : 'images/cancel.png',
'folder' : 'uploads', //要上传到的服务器路径,
'queueID' : 'fileQueue',
'auto' : false, //选定文件后是否自动上传,默认false
'multi' : true, //是否允许同时上传多文件,默认false
'simUploadLimit' : 1, //一次同步上传的文件数目
'sizeLimit' : 19871202, //设置单个文件大小限制,单位为byte
'queueSizeLimit' : 5, //限制在一次队列中的次数(可选定几个文件)。默认值= 999,而一次可传几个文件有 simUploadLimit属性决定。
// 'buttonImg' : 'images/upload.bmp' ,
'fileDesc' : '支持格式:war', //如果配置了以下的'fileExt'属性,那么这个属性是必须的
'fileExt' : '*.war',//允许的格式
// 'scriptData' :{'name':$('#name').val()}, // 多个参数用逗号隔开 'name':$('#name').val(),'num':$('#num').val(),'ttl':$('#ttl').val()
onComplete: function (event, queueID, fileObj, response, data) {
var value = response ;
alert(value);
alert("文件:" + fileObj.name + "上传成功");
},
onError: function(event, queueID, fileObj) {
alert("文件:" + fileObj.name + "上传失败");
},
onCancel: function(event, queueID, fileObj){
alert("取消了" + fileObj.name);
}
});
});
function uploasFile(){
//校验
// var name=document.getElementById("name").value;
// if(name.replace(/\s/g,'') == ''){
// alert("名称不能为空!");
// return false;
// }
///设置 scriptData 的参数
// $('#uploadify').uploadifySettings('scriptData',{'name':$('#name').val()});
//上传
jQuery('#uploadify').uploadifyUpload()
}
</script>
<form enctype="multipart/form-data" action="scripts/uploadify">
<table align="center">
<tr><td> <input type="hidden" id="name" name="name" value=fileObj.name ></td></tr>
<tr><td><input type="file" name="uploadify" id="uploadify" /></td>
<td> <div id="fileQueue"></div> </td></tr>
<tr>
<td></td>
<td><input type="button" value="版本升级" onclick="javascript:uploasFile()">
<input type="button" value="取消升级" onclick="javascript:jQuery('#uploadify').uploadifyClearQueue()"></td>
</tr>
</table>
</form>
</body>
</html>
这个在网上找的自己单独的建一个项目不会出问题,但是整合到我的项目上就会出现
一些问题 request=MultiPartRequestWrapper response=ResponseFacade
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货