编程开发 > JSP > 文章内容

jsp工作总结(7)

2016-10-4编辑:sunny
用jsp上传文件****************************
<%@ page contentType="text/html; charset=gb2312" language="java"  errorPage="errorPage.jsp" %> <%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%><%request.setCharacterEncoding("GB2312");%>
<%String path = "f:\\";int m_totalBytes = 0;m_totalBytes = request.getContentLength();
String type = "";type = (String)request.getContentType();
int totalRead = 0; int readBytes = 0;byte m_binArray[] = new byte[2];
String postedData = "";String suffix = "";m_binArray[0] = 13;m_binArray[1] = 10;
String ctrl = new String(m_binArray); int startPos = 0;int endPos = 0;String msg = "";
if(m_totalBytes != -1){ m_binArray = new byte[m_totalBytes];
for(; totalRead < m_totalBytes; totalRead += readBytes)
readBytes = request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);
postedData = new String(m_binArray, 0, totalRead);ctrl += ctrl;
startPos = postedData.indexOf(ctrl) + 4;int s = postedData.indexOf("filename=");
String tmp = postedData.substring(s + 10 , startPos - 20);s = tmp.indexOf(".");
int e = tmp.indexOf("\""); suffix = tmp.substring(s + 1, e);suffix = suffix.toLowerCase();
if(suffix.equals("gif") || suffix.equals("jpg") || suffix.equals("jpeg"))
{String x = Long.toString(new java.util.Date().getTime());
FileOutputStream f = new FileOutputStream(path + x + "." +suffix);
for(int i = startPos; i < (totalRead - 4); i ++)
{if((m_binArray[i] == 13) && (m_binArray[i + 1] == 10) &&(m_binArray[i + 2] == 45) &&(m_binArray[i + 3] == 45) && (m_binArray[i + 4] == 45))
{endPos = i;break;}}f.write(m_binArray, startPos, endPos - startPos);
msg = "上传成功!";}else{msg = "只能上传gif,jpg,jpeg图片文件!";}}%><html><head>
<title>校友录</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body leftmargin="0" topmargin="0"><div align="center">
<form action="fileUPloadjsp.jsp" method="post" enctype="multipart/form-data" name="form1">
<p><font size="2">文件名:</font> <input name="Image" type="file" id="Image">
</p><p><input type="submit" name="Submit" value="上传">
</p></form><p><%=msg%></p><p><%=m_totalBytes%></p>
<p><%=type%></p><p><%=suffix%></p><p><%=totalRead%></p><p><%=readBytes%></p>
<p><%=postedData%></p><p>----</p><p><%=startPos%></p><p><%=endPos%></p><p>-----</p>
<%for(int i = startPos; i < endPos; i ++){%><%=m_binArray[i]%><%}%><p>------</p>
<%for(int i = 0; i < totalRead; i ++){%><%=m_binArray[i]%><%}%></div></body></html>
jsp工作总结(6)

热点推荐

登录注册
触屏版电脑版网站地图