I'm looking to create a website for uploading photos to MySQL using JSP code, but I'm encountering an error that I'm struggling to resolve in the code.
First, I found an example "upload.html"
<title>Select your File to upload</title>
<form action="loading_blob.jsp" method=POST ENCTYPE="multipart/form-data">
Staff ID:
<input type=text name=id>
Photo:
<input type=file name=uploadfile>
<input type=submit name=button value=upload>
</form>
I then used the example (loading_blob.jsp) and downloaded the cos.jar file, placing it into tomcat->lib.
<%@ page import="java.io.*, java.sql.*, java.util.*, javax.servlet.*, javax.servlet.http.*, com.oreilly.servlet.multipart.*" %>
<%
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
String dirname = "C:\\apache-tomcat-6.0.26\\webapps\\ROOT\\upload-example\\";
File dir = new File(dirname);
String value = "";
String fileName = "";
String filePath = "";
long size = 0;
MultipartParser mp = new MultipartParser(request, 1*1024*1024); // 10MB
Part part;
while ((part = mp.readNextPart()) != null) {
String name = part.getName();
if (part.isParam()) {
// it's a parameter part
if (name.equals("id"))
{
ParamPart paramPart = (ParamPart) part;
value = paramPart.getStringValue();
out.println("param; name=" + name + ", value=" + value);
}
}
else if (part.isFile()) {
// it's a file part
FilePart filePart = (FilePart) part;
fileName = filePart.getFileName();
if (fileName != null) {
filePath = filePart.getFilePath();
// the part actually contained a file
size = filePart.writeTo(dir);
out.println("file; name=" + name + "; filename=" + fileName +
", filePath=" + filePath +
", content type=" + filePart.getContentType() +
", size=" + size);
}
else {
// the field did not contain a file
out.println("file; name=" + name + "; EMPTY");
}
}
}
String dirname1 = dirname + fileName;
out.println(dirname1 + size);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db1", "root", "0000");
InputStream fis = new FileInputStream(dirname1);
PreparedStatement ps = con.prepareStatement("Update person set photo = ?, filename = ? where staff_id = ?");
ps.setBinaryStream (1, fis, size);
ps.setString (2, fileName);
ps.setString (3, value);
ps.executeUpdate();
ps.close();
fis.close();
%>
Error message:
An error occurred at line: 16 in the jsp file: /loading_blob.jsp
The type Part is ambiguous
13: long size = 0;
14:
15: MultipartParser mp = new MultipartParser(request, 1*1024*1024); // 10MB
16: Part part;
17: while ((part = mp.readNextPart()) != null) {
18: String name = part.getName();
19: if (part.isParam()) {
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Thank you