首页 / AJAX / java-JSP AJAX文件上传
java-JSP AJAX文件上传
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-JSP AJAX文件上传,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3384字,纯文字阅读大概需要5分钟。
内容图文
![java-JSP AJAX文件上传](/upload/InfoBanner/zyjiaocheng/673/195d7aeebaed4c489f4108e45c6ebbc9.jpg)
我试图上传一个文件,并使用ajax和jsp将文件的内容显示回浏览器.但是,它对我来说似乎不太好用.
显然,在JSP页面Upload.jsp中,当我尝试从请求中获取getContentType()时,request.getcontentType()== null.
有人对此有经验吗?非常感谢
形成
<form id="uploadform" name="uploadform" enctype="multipart/form-data" action="Upload.jsp" method="post">
<input type="file" name="file" id="listfile" onChange="upload(this.value)"/>
</form>
这是Javascript函数upload(ifile)
function upload(ifile){
if (window.XMLHttpRequest){
//IE7 + and other browsers
xmlhttp = new XMLHttpRequest();
}else{
//IE 6, 5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if(xmlhttp == null){
alert("File Uploading is not available because your browser does not support AJAX");
return;
}
//Function to process response form upload.jsp
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
var response = xmlhttp.responseText;
alert(response);
}
}
xmlhttp.open("POST", "Upload.jsp?file="+ifile, true);
xmlhttp.send(null);
}
这是JSP页面Upload.jsp
<%@page import="java.util.StringTokenizer"%>
<%@page import="java.io.*" %>
<%
response.setContentType("text/html");
response.setHeader("Cache-control", "no-cache");
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
//get length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//convert the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
//decode byte array using default charset
String file = new String(dataBytes);
//Using StringTokenizer to extract genes list
StringTokenizer st = new StringTokenizer(file, " ");
int numtoken = st.countTokens();
for(int i = 0; i < numtoken-1; i++){
st.nextToken();
}
String a = st.nextToken();
st = new StringTokenizer(a, " \n");
numtoken = st.countTokens();
String postlink = "";
st.nextToken();
st.nextToken();
for(int i = 1; i < numtoken-3; i++){
String temp = st.nextToken();
char[] c = temp.toCharArray();
temp = new String(c, 0, c.length-1);
if(!" ".equalsIgnoreCase(temp)){
postlink = postlink + temp + ",";
}
}
String temp = st.nextToken();
postlink = postlink + temp;
out.println(postlink);
out.flush();
out.close();
}else if (contentType == null){
out.println("Not a valid file");
out.flush();
}
%>
解决方法:
尝试以下代码…
HTML:
<form id="uploadform" name="uploadform" enctype="multipart/form-data" action="Upload.jsp" method="post">
<input type="file" name="file" id="listfile" onChange="upload()"/>
</form>
<iframe id="target-iframe" name="target-iframe">
<div id="status">Uploading....</div>
Javascript:
function upload(){
document.getElementById('uploadform').target = 'target_iframe';
document.getElementById('status').style.display="block";
document.getElementById("uploadform").submit();
}
上面的Javascript代码会将提交重定向到iframe(隐藏),而不是重定向到主页本身
JSP:
///do the proccessing in the JSP and in the end output your file content on the some message like follows....
out.println("<script type='text/javascript'>");
out.println("parent.document.getElementById('status').innerHTML=\"<center>SUCCESSFULLY UPLOADED</center>\"; alert('SUCCESSFULLY UPLOADED')");
out.println("</script>");
希望这可以帮助…
内容总结
以上是互联网集市为您收集整理的java-JSP AJAX文件上传全部内容,希望文章能够帮你解决java-JSP AJAX文件上传所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。