Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

MultipartFilter,File upload.

843841Nov 26 2007 — edited Nov 26 2007
Hi,Java masters!!!
I'm using MultipartFiler to upload files from BalusC but I have some probles.
I paste the Filter and configuration in xml from the BalusC balusc.blogspot.com/2007/11/multipartfilter.html .But now I have problem witth Servlet I use.
When I'm using Object fileName = request.getAttribute("file"); I have java.lang.NullPointerException in the next row File newFile = new File((String)fileName);
This is my servlet:

import java.awt.print.Book;

import org.apache.commons.fileupload.FileUploadException;

import webapp.ConnectionManager;
import webapp.DataBaseActions;
import webapp.BookBean;

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.sql.*;

public class checkBook extends HttpServlet {
BookBean book = new BookBean();


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DataBaseActions db=new DataBaseActions();

Connection con;
Statement stmt, stmt1;
System.out.println(request.getSession().getAttribute("user").toString());
String owner = request.getSession().getAttribute("user").toString();

book.setTitle(request.getParameter("Title").trim());
System.out.println(request.getParameter("Title").trim());
book.setAuthor(request.getParameter("Author").trim());
System.out.println(request.getParameter("Author").trim());
book.setPages(request.getParameter("Pages").trim());
System.out.println(request.getParameter("Pages").trim());
book.setCategory(request.getParameter("Category").trim());
System.out.println(request.getParameter("Category").trim());
book.setPublisher(request.getParameter("Publisher").trim());
System.out.println(request.getParameter("Publisher").trim());
book.setYear(request.getParameter("Year"));
book.setUserOwner(owner);

// File upload

Object fileName = request.getAttribute("file");

File newFile = new File((String)fileName);
book.setCounts(String.valueOf(db.getWordsCount(newFile)));
FileReader input = null;

try {
con = ConnectionManager.getConnection();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);


db.createBook( book,newFile);

con.close();
String congratulations = "Congratulations! You have just add a new book!";
request.setAttribute("congratulations", congratulations);
gotoPage("/Library.jsp", request, response);

} catch (SQLException e) {
if(e.getErrorCode()==1){
String error = "There is already a book with the same name!";
request.setAttribute("error", error);
gotoPage("/profile.jsp", request, response);
}
e.printStackTrace();
}
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

public void gotoPage(String address,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
dispatcher.forward(request, response);
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 24 2007
Added on Nov 26 2007
6 comments
366 views