private ServletConfig config; final public void init(ServletConfig config)throws ServletException{ this.config=config; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("The method of the HTML form must be POST"); }
报错: 2007-9-9 20:27:49 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet jsp threw exception org.apache.jasper.JasperException: Unable to compile class for JSP: Stacktrace: at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85) at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330) at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415) at org.apache.jasper.compiler.Compiler.compile(Compiler.java:308) at org.apache.jasper.compiler.Compiler.compile(Compiler.java:286) at org.apache.jasper.compiler.Compiler.compile(Compiler.java:273) at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445) at java.lang.Thread.run(Thread.java:595)
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class UploadHandle {private ServletInputStream in = null;
private ServletOutputStream out = null;
private HttpServletRequest request = null;
private HttpServletResponse response = null;private String boundary = "";
private String strEndFlag = "";
private boolean bStreamNotEnd = true;
private static String savePath = "./upload/";
private int fileSize = 0;private long maxFileSize = 20000000;
public UploadHandle(HttpServletRequest request, HttpServletResponse
response, String savePath2) {
this.request = request;
this.response = response;
try {
in = request.getInputStream();
out = response.getOutputStream();if (savePath2 != null && !savePath2.equals("")) {
savePath = savePath2;
} else {
String strAppPath = System.getProperty("user.dir");
savePath = strAppPath + "/upload/";
}
File file = new File(savePath);
file.mkdirs();
file = null;
} catch (Exception e) {
}}public void setMaxFileSize(long maxFileSize) {
this.maxFileSize = maxFileSize;
}public void go() throws Exception {
String strContentType = request.getContentType();
boundary = "--" +
strContentType.substring(strContentType.indexOf("boundary=") + 9);
strEndFlag = boundary + "--";String strParameterName = "";
String strParameterValue = "";
String strFileName = "";
String[] strArrFiles = new String[]{"", "", "", ""};String strLine = "";Matcher matcher = null;
String strPattern = "";
strPattern = "Content-Disposition: form-data;
name=\"([\\p{Print}&&[^\"]]+)\"(; filename=\"(.+)\")?";
bStreamNotEnd = true;
while (bStreamNotEnd && !(strLine = readLine()).equals(strEndFlag)) {
matcher = Pattern.compile(strPattern,
Pattern.CASE_INSENSITIVE).matcher(strLine);
if (matcher.find()) {
strParameterName = matcher.group(1);
strFileName = matcher.group(3);
// System.out.println("strParameterName: " + strParameterName);
// System.out.println("strFileName: " + strFileName);while (!this.readLine().equals("")){}if (strFileName != null) {
strFileName = strFileName.replace("\\", "/");
if(strFileName.lastIndexOf("/") > -1) {
strFileName = strFileName.substring(strFileName.lastIndexOf("/") + 1);
}
String str2 = "";
if(strFileName.indexOf(".") > -1) {
str2 = strFileName.substring(strFileName.lastIndexOf("."));
}strFileName = (new DateUtil()).getDateTime().replace(":", "_").replace("
", "_") + "_" + ((int) (Math.random() * 10000000)) + "" + str2; //(new
DateUtil()).getDateTime().replace(":", "'").replace(" ", "_") + "_" +String filePath = savePath + strFileName;
saveFile(filePath);if (fileSize > 0) {
String strTemp = (String)request.getAttribute(strParameterName);
if (strTemp != null && !strTemp.equals("")) {
request.setAttribute(strParameterName, strTemp + ";/" + filePath);
String strTemp2 = (String)request.getAttribute("countSum");
request.setAttribute("fileSize", strTemp2 + ";/" + fileSize);
} else {
request.setAttribute(strParameterName, filePath);
request.setAttribute("fileSize", fileSize);
}
}
} else {
strLine = readLine(); // 读取参数值
strParameterValue = strLine;
// System.out.println("strParameterValue: " + strParameterValue);
String strTemp = (String)request.getAttribute(strParameterName);
if (strTemp != null && !strTemp.equals("")) {
request.setAttribute(strParameterName, strTemp + ";/" + strParameterValue);
} else {
request.setAttribute(strParameterName, strParameterValue);
}
}
}
}
}private String readLine() throws Exception {
String theResult = "";ByteArrayOutputStream baos = new ByteArrayOutputStream();
int iByte = 0;
while ((iByte = in.read()) != -1) {
baos.write(iByte);
if (iByte == 10) {
theResult = baos.toString().replaceAll("[\r\n]", "");
break;
}
}// System.out.println(theResult);
return theResult;
}private Object[] getLineDataWithRtLf() throws Exception {
// buffer, readCount, lnFlag
Object[] theResult = null;
int bufferSize = 128;
byte[] buffer = new byte[bufferSize];
int readCount = 0;
boolean lnFlag = false;int iByte = 0;
int index = 0;
while ((iByte = in.read()) != -1) {
// System.out.print((char)iByte);
buffer[index] = (byte)iByte;
if (iByte == 10) {
readCount = index + 1;
lnFlag = true;
break;
}
if (index == bufferSize - 1) {
readCount = bufferSize;
break;
}
index++;
}theResult = new Object[]{buffer, String.valueOf(readCount),
String.valueOf(lnFlag)};
return theResult;
}private void saveFile(String filePath) throws Exception {
fileSize = 0;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(filePath);
bos = new BufferedOutputStream(fos, 4096);
} catch(Exception e) {
e.printStackTrace();
}Object[] objArrTemp = null;
byte[] buffer_1 = null;
byte[] buffer_2 = null;
int readCount_1 = 0;
int readCount_2 = 0;
boolean lnFlag = false;objArrTemp = this.getLineDataWithRtLf();
buffer_1 = (byte[])objArrTemp[0];
readCount_1 = Integer.parseInt((String)objArrTemp[1]);while (true) {
objArrTemp = this.getLineDataWithRtLf();
buffer_2 = (byte[])objArrTemp[0];
readCount_2 = Integer.parseInt((String)objArrTemp[1]);
lnFlag = Boolean.parseBoolean((String)objArrTemp[2]);if (lnFlag == true) {
String strTemp = new String(buffer_2, 0, readCount_2);if (strTemp.indexOf(boundary) > -1) {
// System.out.print(new String(buffer_1, 0, readCount_1 - 2));
try {
fileSize += readCount_1;
if (fileSize > maxFileSize) {
bos.close();
throw new Exception("文件太大。fileSize > " + maxFileSize);
}
bos.write(buffer_1, 0, readCount_1 - 2);
}catch(Exception e){throw e;}
if (strTemp.indexOf(strEndFlag) > -1) {
this.bStreamNotEnd = false;
}
break;
}
}
// System.out.print(new String(buffer_1, 0, readCount_1));fileSize += readCount_1;
if (fileSize > maxFileSize) {
bos.close();
throw new Exception("文件太大。fileSize > " + maxFileSize);
}
bos.write(buffer_1, 0, readCount_1);buffer_1 = buffer_2;
readCount_1 = readCount_2;
}try {
bos.flush();
fos.close();
} catch(Exception e) {
e.printStackTrace();
}
}}
<!DOCTYPE HTML PUBLIC "_//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>将图象以二进制数据格式存储到数据库中</title>
<style type="text/css"><!--
.style1 {
color: #FF0000;
font-size: 24px;
}
--></style>
</head>
<body>
<div align="center">
<span class="style1">将图象以二进制数据格式存储到数据库中</span><BR></div>
<hr>
<BR>
<body>
<form name="form1" method="post" action="testimage.jsp">
<p align="center">请输入图片id:
<input type="text" name="id">
</P>
<p align="center">请输入图片的URL:
<input type="text" name="image">
</P>
<p align="center">
<input type="submit" name="Submit" value="提交">
</P>
</form>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>读取数据库中的二进制文件</title>
<style type="text/css">
<!--
body {
background-color: #FFFFCC;
}
-->
</style></head>
<body>
<div align="center">
<p> </p>
<p> </p>
<p>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();//加JDBC驱动程序
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bin_db";String user="sa";
String password="123";
Connection conn= DriverManager.getConnection(url,user,password);
String sql="select pic from picture where id=2";
Statement stmt=stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
try{
while(rs.next())
{ response.setContentType("image/jpeg");//设置图象返回的类型
ServletOutputStream sout=response.getOutputStream();
InputStream in=rs.getBinaryStream(1);
byte b[]=new byte[0x7a120];
while(in.read(b)!=-1)
{
sout.write(b);
}
sout.flush();
sout.close();
}
}catch(Exception e){}
%>
</p>
</div>
</body>
</html>
<FORM ENCTYPE="MULTIPART/FORM-DATA" METHOD="POST" ACTION="/test/servletUpload">
文件名称:
<INPUT TYPE="file" NAME="ulfile">
<BR>存储到服务器上的路径:
<INPUT TYPE="TEXT" name="PATH"><BR>
<INPUT TYPE="submit" value="上传">
<INPUT TYPE="reset" value="清除">
</FORM>
</BODY></HTML>
下面是servletUpload(这个是servlet)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.*;public class servletUpload extends HttpServlet {
private ServletConfig config;
final public void init(ServletConfig config)throws ServletException{
this.config=config;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("The method of the HTML form must be POST");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
PrintWriter out=response.getWriter();
int count=1;
SmartUpload mySmartUpload=new SmartUpload();
try{
mySmartUpload.initialize(config,request,response);
mySmartUpload.upload();
mySmartUpload.save(mySmartUpload.getRequest().getParameter("PATH"));
out.println(count+"file uploaded.");
}catch(Exception e){
out.println("Unable to upload the file.<br>");
out.println("Error:"+e.toString());
}
}}
2007-9-9 20:27:49 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Thread.java:595)