jspsmartupload上传大于50k文件出错 出错信息是什么?jspsmartupload好像就设计了这个限制,我从不用它,自己写上传代码,想怎么改就怎么改。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 hoho~upload.java--------------------- import java.io.*;import java.util.Date;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.jsp.PageContext;public class uploadfile extends HttpServlet{ protected byte m_binArray[] = null; protected HttpServletRequest m_request = null; protected HttpServletResponse m_response = null; protected ServletContext m_application = null; private int m_totalBytes = 0; private int m_currentIndex = 0; private int m_startData = 0; private int m_endData = 0; private String m_boundary = null; private String m_savePath = new String(""); public uploadfile() { m_totalBytes = 0; m_currentIndex = 0; m_startData = 0; m_endData = 0; m_boundary = new String(); } public final void init(ServletConfig config) throws ServletException { m_application = config.getServletContext(); } public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { m_request = request; m_response = response; } public final void initialize(PageContext pageContext) throws ServletException { m_application = pageContext.getServletContext(); m_request = (HttpServletRequest)pageContext.getRequest(); m_response = (HttpServletResponse)pageContext.getResponse(); } public void uploadData() throws IOException, ServletException { int totalRead = 0; int readBytes = 0; boolean found = false; String dataHeader = new String(); String fieldName = new String(); System.out.println("---------用户开始上传文件-----------"); System.out.println("用户IP:"+m_request.getRemoteAddr()); java.io.FileOutputStream fileout=null; boolean isFile = false; m_totalBytes = m_request.getContentLength(); m_binArray = new byte[m_totalBytes]; for(; totalRead < m_totalBytes; totalRead += readBytes) try { m_request.getInputStream(); readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead); } catch(Exception e) { System.out.println("获取数据失败,发生时间:"+new Date().toLocaleString()); System.out.println(e.toString()); } System.out.println("数据大小:"+totalRead); for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++) { if(m_binArray[m_currentIndex] == 13) found = true; else m_boundary = m_boundary + (char)m_binArray[m_currentIndex]; } if(m_currentIndex == 1) return; m_currentIndex++; do { if(m_currentIndex >= m_totalBytes) break; dataHeader = getDataHeader(); m_currentIndex = m_currentIndex + 2; isFile = dataHeader.indexOf("filename") > 0; fieldName = getDataFieldValue(dataHeader, "name"); getDataSection(); if(isFile) { try { fileout = new FileOutputStream(getFilename(dataHeader)); fileout.write(m_binArray,m_startData,m_endData-m_startData+1); System.out.println("写入数据文件:"+getFilename(dataHeader)+"成功 ,时间" + new Date().toLocaleString()); } catch(Exception e) { System.out.println("写入文件失败,发生时间:"+new Date().toLocaleString()); System.out.println(e.toString()); } finally { try { fileout.close(); } catch(Exception e) { System.out.println(e.toString()); } } } if((char)m_binArray[m_currentIndex + 1] == '-') break; m_currentIndex = m_currentIndex + 2; } while(true); System.out.println("用户数据上传完毕,结束时间:"+new Date().toLocaleString()); } private String getDataFieldValue(String dataHeader, String fieldName) { String token = new String(); String value = new String(); int pos = 0; int i = 0; int start = 0; int end = 0; token = String.valueOf((new StringBuffer(String.valueOf(fieldName))).append("=").append('"')); pos = dataHeader.indexOf(token); if(pos > 0) { i = pos + token.length(); start = i; token = "\""; end = dataHeader.indexOf(token, i); if(start > 0 && end > 0) value = dataHeader.substring(start, end); } return value; } private String getDataHeader() { int start = m_currentIndex; int end = 0; int len = 0; boolean found = false; while(!found) if(m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2] == 13) { found = true; end = m_currentIndex - 1; m_currentIndex = m_currentIndex + 2; } else { m_currentIndex++; } String dataHeader = new String(m_binArray, start, (end - start) + 1); return dataHeader; } private void getDataSection() { boolean found = false; String dataHeader = new String(); int searchPos = m_currentIndex; int keyPos = 0; int boundaryLen = m_boundary.length(); m_startData = m_currentIndex; m_endData = 0; do { if(searchPos >= m_totalBytes) break; if(m_binArray[searchPos] == (byte)m_boundary.charAt(keyPos)) { if(keyPos == boundaryLen - 1) { m_endData = ((searchPos - boundaryLen) + 1) - 3; break; } searchPos++; keyPos++; } else { searchPos++; keyPos = 0; } } while(true); m_currentIndex = m_endData + boundaryLen + 3; } private String getFileExt(String fileName) { String value = new String(); int start = 0; int end = 0; if(fileName == null) return null; start = fileName.lastIndexOf(46) + 1; end = fileName.length(); value = fileName.substring(start, end); if(fileName.lastIndexOf(46) > 0) return value; else return ""; } private String getContentType(String dataHeader) { String token = new String(); String value = new String(); int start = 0; int end = 0; token = "Content-Type:"; start = dataHeader.indexOf(token) + token.length(); if(start != -1) { end = dataHeader.length(); value = dataHeader.substring(start, end); } return value; } private String getTypeMIME(String ContentType) { String value = new String(); int pos = 0; pos = ContentType.indexOf("/"); if(pos != -1) return ContentType.substring(1, pos); else return ContentType; } private String getSubTypeMIME(String ContentType) { String value = new String(); int start = 0; int end = 0; start = ContentType.indexOf("/") + 1; if(start != -1) { end = ContentType.length(); return ContentType.substring(start, end); } else { return ContentType; } } private String getFilename(String dataHeader) { String filenametemp = getDataFieldValue(dataHeader, "filename"); if(m_savePath.lastIndexOf("\\")!=0) m_savePath+="\\"; return m_savePath+filenametemp.substring(filenametemp.lastIndexOf("\\")+1); } public void setSavePath(String path) { m_savePath = path; }} --------upload.html--------<html><meta http-equiv="Content-Language" content="zh-cn"><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><head><title>上传文件</title></head><body bgcolor="#FFFFFF"><FORM method="post" enctype="multipart/form-data" action="FileUpload.jsp" ><INPUT name="filea" type="file"><BR><INPUT name="btnval" type="submit" value="上载"><INPUT type="reset" value="重置"> </FORM></body></html>--FileUpload.jsp--<%@ page language="java" pageEncoding="GB2312" %><jsp:useBean id="upload" scope="page" class="uploadfile" /><% upload.initialize(pageContext); upload.uploadData(); out.print("上传完毕!");%>祝你好运..下次提问前可以先搜索一下CSDN. :)--------upload.html--------<html><meta http-equiv="Content-Language" content="zh-cn"><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><head><title>上传文件</title></head><body bgcolor="#FFFFFF"><FORM method="post" enctype="multipart/form-data" action="FileUpload.jsp" ><INPUT name="filea" type="file"><BR><INPUT name="btnval" type="submit" value="上载"><INPUT type="reset" value="重置"> </FORM></body></html>--FileUpload.jsp--<%@ page language="java" pageEncoding="GB2312" %><jsp:useBean id="upload" scope="page" class="uploadfile" /><% upload.initialize(pageContext); upload.uploadData(); out.print("上传完毕!");%>祝你好运..下次提问前可以先搜索一下CSDN. :) 字符串转换问题,求最佳答案· 问下servlet-api-2.4.jar jsp-api-2.1.jar这2个包做什么用的? 异机运行程序出错 jdbc连接池怪问题! 实现图片的变换功能,急!!在线等 window.open ()的问题 jsp页面框架中的jsp文件如何接收传给父窗口的参数? 问大家一个问题就是在关闭页时(window.close())如何不出现WINDOW的提示框? Jsp页面应放在什么目录下 获得下拉列表全部option的value问题(在线) 关于在网页重定向到一个servlet页面的问题? 框架页面之间如何传递参数?
---------------------
import java.io.*;
import java.util.Date;import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.jsp.PageContext;
public class uploadfile extends HttpServlet
{ protected byte m_binArray[] = null;
protected HttpServletRequest m_request = null;
protected HttpServletResponse m_response = null;
protected ServletContext m_application = null;
private int m_totalBytes = 0;
private int m_currentIndex = 0;
private int m_startData = 0;
private int m_endData = 0;
private String m_boundary = null;
private String m_savePath = new String(""); public uploadfile()
{
m_totalBytes = 0;
m_currentIndex = 0;
m_startData = 0;
m_endData = 0;
m_boundary = new String();
} public final void init(ServletConfig config) throws ServletException
{
m_application = config.getServletContext();
} public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
m_request = request;
m_response = response;
} public final void initialize(PageContext pageContext)
throws ServletException
{
m_application = pageContext.getServletContext();
m_request = (HttpServletRequest)pageContext.getRequest();
m_response = (HttpServletResponse)pageContext.getResponse();
} public void uploadData() throws IOException, ServletException
{
int totalRead = 0;
int readBytes = 0;
boolean found = false;
String dataHeader = new String();
String fieldName = new String(); System.out.println("---------用户开始上传文件-----------");
System.out.println("用户IP:"+m_request.getRemoteAddr()); java.io.FileOutputStream fileout=null;
boolean isFile = false;
m_totalBytes = m_request.getContentLength();
m_binArray = new byte[m_totalBytes];
for(; totalRead < m_totalBytes; totalRead += readBytes)
try
{
m_request.getInputStream();
readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);
}
catch(Exception e)
{
System.out.println("获取数据失败,发生时间:"+new Date().toLocaleString());
System.out.println(e.toString());
}
System.out.println("数据大小:"+totalRead);
for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++)
{
if(m_binArray[m_currentIndex] == 13)
found = true;
else
m_boundary = m_boundary + (char)m_binArray[m_currentIndex];
}
if(m_currentIndex == 1)
return;
m_currentIndex++;
do
{
if(m_currentIndex >= m_totalBytes)
break;
dataHeader = getDataHeader();
m_currentIndex = m_currentIndex + 2;
isFile = dataHeader.indexOf("filename") > 0;
fieldName = getDataFieldValue(dataHeader, "name");
getDataSection();
if(isFile)
{
try
{
fileout = new FileOutputStream(getFilename(dataHeader));
fileout.write(m_binArray,m_startData,m_endData-m_startData+1);
System.out.println("写入数据文件:"+getFilename(dataHeader)+"成功 ,时间" + new Date().toLocaleString());
}
catch(Exception e)
{
System.out.println("写入文件失败,发生时间:"+new Date().toLocaleString());
System.out.println(e.toString());
}
finally
{
try
{
fileout.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
} if((char)m_binArray[m_currentIndex + 1] == '-')
break;
m_currentIndex = m_currentIndex + 2;
}
while(true);
System.out.println("用户数据上传完毕,结束时间:"+new Date().toLocaleString());
} private String getDataFieldValue(String dataHeader, String fieldName)
{
String token = new String();
String value = new String();
int pos = 0;
int i = 0;
int start = 0;
int end = 0;
token = String.valueOf((new StringBuffer(String.valueOf(fieldName))).append("=").append('"'));
pos = dataHeader.indexOf(token);
if(pos > 0)
{
i = pos + token.length();
start = i;
token = "\"";
end = dataHeader.indexOf(token, i);
if(start > 0 && end > 0)
value = dataHeader.substring(start, end);
}
return value;
} private String getDataHeader()
{
int start = m_currentIndex;
int end = 0;
int len = 0;
boolean found = false;
while(!found)
if(m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2] == 13)
{
found = true;
end = m_currentIndex - 1;
m_currentIndex = m_currentIndex + 2;
}
else
{
m_currentIndex++;
} String dataHeader = new String(m_binArray, start, (end - start) + 1);
return dataHeader;
} private void getDataSection()
{
boolean found = false;
String dataHeader = new String();
int searchPos = m_currentIndex;
int keyPos = 0;
int boundaryLen = m_boundary.length();
m_startData = m_currentIndex;
m_endData = 0;
do
{
if(searchPos >= m_totalBytes)
break;
if(m_binArray[searchPos] == (byte)m_boundary.charAt(keyPos))
{
if(keyPos == boundaryLen - 1)
{
m_endData = ((searchPos - boundaryLen) + 1) - 3;
break;
}
searchPos++;
keyPos++;
}
else
{
searchPos++;
keyPos = 0;
}
}
while(true);
m_currentIndex = m_endData + boundaryLen + 3;
} private String getFileExt(String fileName)
{
String value = new String();
int start = 0;
int end = 0;
if(fileName == null)
return null;
start = fileName.lastIndexOf(46) + 1;
end = fileName.length();
value = fileName.substring(start, end);
if(fileName.lastIndexOf(46) > 0)
return value;
else
return "";
} private String getContentType(String dataHeader)
{
String token = new String();
String value = new String();
int start = 0;
int end = 0;
token = "Content-Type:";
start = dataHeader.indexOf(token) + token.length();
if(start != -1)
{
end = dataHeader.length();
value = dataHeader.substring(start, end);
}
return value;
} private String getTypeMIME(String ContentType)
{
String value = new String();
int pos = 0;
pos = ContentType.indexOf("/");
if(pos != -1)
return ContentType.substring(1, pos);
else
return ContentType;
} private String getSubTypeMIME(String ContentType)
{
String value = new String();
int start = 0;
int end = 0;
start = ContentType.indexOf("/") + 1;
if(start != -1)
{
end = ContentType.length();
return ContentType.substring(start, end);
}
else
{
return ContentType;
}
}
private String getFilename(String dataHeader)
{
String filenametemp = getDataFieldValue(dataHeader, "filename");
if(m_savePath.lastIndexOf("\\")!=0)
m_savePath+="\\";
return m_savePath+filenametemp.substring(filenametemp.lastIndexOf("\\")+1);
}
public void setSavePath(String path)
{
m_savePath = path;
}
}
--------
upload.html
--------
<html>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<head>
<title>上传文件</title>
</head>
<body bgcolor="#FFFFFF"><FORM method="post" enctype="multipart/form-data" action="FileUpload.jsp" >
<INPUT name="filea" type="file"><BR>
<INPUT name="btnval" type="submit" value="上载">
<INPUT type="reset" value="重置">
</FORM></body>
</html>
--
FileUpload.jsp
--
<%@ page language="java" pageEncoding="GB2312" %>
<jsp:useBean id="upload" scope="page" class="uploadfile" />
<%
upload.initialize(pageContext);
upload.uploadData();
out.print("上传完毕!");
%>祝你好运..下次提问前可以先搜索一下CSDN. :)--------
upload.html
--------
<html>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<head>
<title>上传文件</title>
</head>
<body bgcolor="#FFFFFF"><FORM method="post" enctype="multipart/form-data" action="FileUpload.jsp" >
<INPUT name="filea" type="file"><BR>
<INPUT name="btnval" type="submit" value="上载">
<INPUT type="reset" value="重置">
</FORM></body>
</html>
--
FileUpload.jsp
--
<%@ page language="java" pageEncoding="GB2312" %>
<jsp:useBean id="upload" scope="page" class="uploadfile" />
<%
upload.initialize(pageContext);
upload.uploadData();
out.print("上传完毕!");
%>祝你好运..下次提问前可以先搜索一下CSDN. :)