Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
dear sir,

I have written a business logic in servlet :
Java
  1  package com.rfmsSite.upload;
  2  
  3  import java.io.FileNotFoundException;
  4  import java.io.IOException;
  5  import java.io.InputStream;
  6  import java.io.PrintWriter;
  7  import java.sql.Connection;
  8  import java.sql.PreparedStatement;
  9  import java.sql.SQLException;
 10  import java.sql.Statement;
 11  
 12  import javax.servlet.ServletException;
 13  import javax.servlet.annotation.MultipartConfig;
 14  import javax.servlet.annotation.WebServlet;
 15  import javax.servlet.http.HttpServlet;
 16  import javax.servlet.http.HttpServletRequest;
 17  import javax.servlet.http.HttpServletResponse;
 18  import javax.servlet.http.Part;
 19  
 20  import org.apache.coyote.http11.InputFilter;
 21  
 22  import com.rfmsSite.util.DBhelper;
 23  
 24  @WebServlet("/uploadBlob")
 25  @MultipartConfig
 26  public class UploadServlet extends HttpServlet{
 27  	public static Part filepart=null;
 28  	public static Connection conn =null;
 29  	public static PreparedStatement stmt = null;
 30  	public static final String upload_xls = "insert into attachment values(?);";
 31  	@Override
 32  	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 33  		// TODO Auto-generated method stub
 34  		PrintWriter out = resp.getWriter();
 35  		resp.setContentType("text/html");
 36  		
 37  		filepart = req.getPart("Upload file");
 38  		InputStream excl_bytes = null;
 39  		try
 40  		{
 41  		  if(!filepart.getContentType().equals("application/vnd.ms-excel")) //line 41
 42  		  {
 43  			out.println("<br> invalid file format");  
 44  			return;
 45  		  }
 46  		  else if(filepart.getSize()>10240000)
 47  		  {
 48  			  out.println("<br> File size too big");
 49                return;
 50  		  }
 51  		  
 52  		  excl_bytes = filepart.getInputStream();
 53  		  final byte[] bytes = new byte[excl_bytes.available()];
 54  		  excl_bytes.read(bytes);
 55  		  try
 56  		  {
 57  			  conn = DBhelper.createConnection();
 58  			  int success=0;
 59  			  stmt = conn.prepareStatement(upload_xls);
 60  			  stmt.setBytes(2, bytes);
 61  			  success = stmt.executeUpdate();
 62  			  if(success>=1)
 63  			  {
 64  				  out.println("br>File successfully stored");
 65  			  }
 66  		  }
 67  			  catch (SQLException e) {
 68  			  out.println(e);
 69  			  System.exit(0);
 70  		  }
 71  			// TODO: handle exception
 72  		  }
 73  		  
 74  		  finally
 75  		  {
 76  			  if(excl_bytes!=null)
 77  			  {
 78  				  excl_bytes.close();
 79  			  }
 80  			  
 81  			  if(out!=null)
 82  			  {
 83  				  out.close();
 84  			  }
 85  		  }
 86  	}
 87  }

here is:
jsp page code:
ASP.NET
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
    <title>File Upload</title>
</head>
<body>
    <a href="Home.html">Home</a>
    <a href="ExcelServlet">Excel data</a>
    <hr>
    <form action="uploadBlob" method="post" enctype="multipart/form-data">
        <table>
        <tr>
            <td><input type="file" name="file1"><td>
        </tr>
        <tr><td colspan="2">
            <input type="submit" value="Upload file" name="upload"/>
       </table>
    </form>
</body>
</html>

when I run this application , then this exception occurs:
application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet


What I have tried:

Java
if(!filepart.getContentType().equals("application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
Posted
Updated 19-Mar-20 7:12am
v2
Comments
ZurdoDev 19-Mar-20 7:56am    
Something is null. All you have to do is debug it and find what line of code is causing the error and what on that line is null. Then figure out how to fix it.

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Member 14639038 19-Mar-20 12:10pm    
i know this is due to mime extension
You are calling req.getPart("Upload file");, but your <input> has name="file1".

I suspect the name passed to getPart needs to match the name attribute on the <input>:
Java
filepart = req.getPart("file1");
Also your alternative MIME type is incorrect - you won't have two application/ prefixes.
Java
if(!filepart.getContentType().equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
 
Share this answer
 
Comments
Member 14639038 20-Mar-20 14:06pm    
what will be the mime type for excel with .xslx and .xls both
Richard Deeming 23-Mar-20 5:58am    
The MIME type for .xlsx is application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.

The MIME type for .xls is application/vnd.ms-excel.

You will need to check both; there is no magic string which will match both file types.
Member 14639038 23-Mar-20 14:05pm    
k ! sir ! i will try it .
but i want both formats to be accept.
bt how i can do it
Richard Deeming 23-Mar-20 14:19pm    
Then you'll need to test for both.
Member 14639038 21-Mar-20 1:34am    
i have not idea about that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900