• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error In uploading a Photo by jsp in Photo Gallery Project

 
S.shashank shekhar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Jsp Code:-

<%--
Document : photoupload
Created on : 10 May, 2015, 7:00:15 PM
Author : sony
--%>
<%@page import="java.net.SocketException"%>
<%@page import="java.net.UnknownHostException"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.tomcat.util.http.fileupload.FileItem"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.apache.tomcat.util.http.fileupload.FileUploadException"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.tomcat.util.http.fileupload.FileItemFactory"%>
<%@page import="org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Statement"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Photo Gallery</title>
</head>
<body bgcolor="red" background="images1/Wallpaper.jpg" >
<% try {
String FileToUpload = "", filepath = "", id="";
String s1=(String)session.getAttribute("shekhar1");
java.util.Date date=new java.util.Date();
String d =date.toString();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
}
catch (FileUploadException e)

{
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
String itemName = item.getFieldName();

} else {
String itemName = item.getName();

//String userFieldName = item.getFieldName();
if (itemName != null) {
if (itemName.equals("")) {
out.println("No File Uploaded");
} else {
int idx = itemName.lastIndexOf("\\");
if (idx > -1) {
//in case of IE, it sends complete path. Hence only file name must be filtered out.
idx = idx + 1;
FileToUpload = itemName.substring(idx);
} else {
//in case of other borwsers, its just filename
FileToUpload = itemName;
}
boolean folderMade = (new File("C:/Users/sony/Documents/NetBeansProjects/PhotoGalleryFinal/web/uploadimg")).mkdirs();
filepath = "C:/Users/sony/Documents/NetBeansProjects/PhotoGalleryFinal/web/uploadimg/" + FileToUpload;
File savedFile = new File(filepath);
item.write(savedFile);
}
}
}//else
}
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "shashank");

PreparedStatement ps = con.prepareStatement("insert into upload values(?,?,?,?)");
ps.setString(1, s1);
ps.setString(2, id);
ps.setString(3, FileToUpload);
ps.setString(4,d);
int i = ps.executeUpdate();

if (i > 0) {
response.sendRedirect("viewgallery.jsp");
//out.println("uploaded");
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>


How to Solve this Error Please help me .......................
Error :-
Capturefb.PNG
[Thumbnail for Capturefb.PNG]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code really should be in a Servlet or Java class.

In any case, what is the error message you are getting?
 
S.shashank shekhar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This error comes in netbeans ide while uploading aa image......
jsp.PNG
[Thumbnail for jsp.PNG]
Error message
 
K. Tsang
Bartender
Posts: 3604
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Tomcat API, the parseRequest() is from FileUploadBase which takes in a RequestContext (an interface).

There is a class ServletRequestContext that takes in a HTTPServletRequestContext

You may need to use that instead.

Actually why are you using Tomcat file upload API? There are much more common and well documented API like Apache Commons upload or even Java EE 6 API support file upload here
 
S.shashank shekhar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why this red error comes please help me ..............
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tsang explained why you are getting the error. You need to pass in a HTTPServletRequestContext instead of a request:

K. Tsang wrote:According to the Tomcat API, the parseRequest() is from FileUploadBase which takes in a RequestContext (an interface).

There is a class ServletRequestContext that takes in a HTTPServletRequestContext
 
S.shashank shekhar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem Solved
Thanks Jeanne Boyarsky ,K. Tsang
 
S.shashank shekhar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
items = upload.parseRequest(new ServletRequestContext(request));
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!