我用的是uploadify 3.0.0版本的,在网上看了别人写的代码,代码链接是http://blog.csdn.net/zb0567/archive/2011/04/25/6362795.aspx。
现在点击上传,报the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded和java.lang.NullPointerException异常。我用的是Eclipse.请高手赶紧帮个忙啊~解决方案也可以发我邮箱:[email protected]
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>测试</title>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<link href="css/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.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : 'uploadify.swf',
'script' : 'servlet/Upload',
'cancelImg' : '/images/uploadify-cancel.png',
'folder' : '/uploads',
'queueID' : 'fileQueue',
'auto' : false,
'multi' : true,
'simUploadLimit' : 2,
'buttonText' : 'BROWSE',
'method' :'post'
});
});
</script>
</head>
<body>
<div id="fileQueue"></div>
<input type="file" name="uploadify" id="uploadify" />
<p><br><br>
<a href="javascript:jQuery('#uploadify').uploadifyUpload()">开始上传</a>
<a href="javascript:jQuery('#uploadify').uploadifyCancel()">取消所有上传</a>
</p>
</body>
</html>
Servlet代码
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String savePath = this.getServletConfig().getServletContext()
.getRealPath("");
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 {
System.out.println(request.getQueryString());
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
ex.printStackTrace();
}
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);
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
现在点击上传,报the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded和java.lang.NullPointerException异常。我用的是Eclipse.请高手赶紧帮个忙啊~解决方案也可以发我邮箱:[email protected]
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>测试</title>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<link href="css/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.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : 'uploadify.swf',
'script' : 'servlet/Upload',
'cancelImg' : '/images/uploadify-cancel.png',
'folder' : '/uploads',
'queueID' : 'fileQueue',
'auto' : false,
'multi' : true,
'simUploadLimit' : 2,
'buttonText' : 'BROWSE',
'method' :'post'
});
});
</script>
</head>
<body>
<div id="fileQueue"></div>
<input type="file" name="uploadify" id="uploadify" />
<p><br><br>
<a href="javascript:jQuery('#uploadify').uploadifyUpload()">开始上传</a>
<a href="javascript:jQuery('#uploadify').uploadifyCancel()">取消所有上传</a>
</p>
</body>
</html>
Servlet代码
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String savePath = this.getServletConfig().getServletContext()
.getRealPath("");
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 {
System.out.println(request.getQueryString());
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
ex.printStackTrace();
}
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);
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
解决方案 »
- 关于JavaScript的一个问题,等待高手解答
- struts2零配置问题
- sql在oracle中可以运行,到Hibernate中有错。(功能过滤重复记录功能)
- 有人遇到这样类似的问题吗?求助
- 关于SPRING的配置问题,想知道如果使用了JAVABEAN文件操作SPRING封装的JDBC,SPRING应该怎么配置这个文件啊,急啊!!!
- 哪里有jaxp.jar下,急~~~~~~~~~~~~~~~~~~~~~~~~~~
- 小弟学了点j2se,想继续学习j2ee
- ejb和j2ee是什么关系?
- hibernate4.0不再需要annotation了吗?
- Java编程,求大佬
- 云计算到底是什么啊?
- strtus2如何下载ftp上的文件
擦。。我2了,忘了这是用jquery插件写的ajax
附上我的
$("#uploadTest").uploadify({
'uploader' : '<%=basePath%>/swf/uploadify.swf',
'script' : 'upload.action',
'cancelImg' : '<%=basePath%>/img/cancel.png',
'fileDataName' : 'myFile',
'queueID' : 'fileQueue',
//'folder' : '/upload',
'auto' : false,
'buttonText' : "浏览",
'simUploadLimit' : 20,
'sizeLimit' : 999999999999,
'queueSizeLimit' : 20,
'fileDesc' : '支持格式:jpg,gif,png,bmp',
'fileExt' : '*.jpg;*.gif,*.jpeg,*.png,*.bmp',
'onComplete': function (event, queueID, fileObj, response, data) {
//alert(fileObj.name+" "+response);
$("#img1").attr("src","../../"+response);
$("#h1").attr("href","../../"+response);
$("#img").attr("value",response);
}
});