I like this simple code from ajajava.com on how to read and traverse a ZIP file.
ZipFile zipFile = new ZipFile("test.zip");
Enumeration<?> enu = zipFile.entries();
while (enu.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enu.nextElement();
String name = zipEntry.getName();
long size = zipEntry.getSize();
long compressedSize = zipEntry.getCompressedSize();
System.out.printf("name: %-20s | size: %6d | compressed size: %6d\n", name, size, compressedSize);
However, my problem is that the ZIP file in my application is uploaded via HTML so I can't pass a string or a File object to the ZipFile object. My ZIP file is of type Part. and if I do this:
ZipFile zipFile = new ZipFile((File) uploadedZIP);
I receive this run time error:
java.lang.ClassCastException: org.apache.catalina.fileupload.PartItem cannot be cast to java.io.File
My applications allows the user to:
upload a single image - works fine
2.upload a ZIP file of many images - have a problem with
This is my HTML:
<%--
Document : index
Created on : Jul 26, 2016, 1:13:25 PM
Author : bnbih
--%>
<%@page import = "java.sql.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="post" action="uploadServlet" enctype="multipart/form-data">
<table border="0">
<tr>
<td>Single Student Portrait Photo: </td>
<td><input type="file" name="photo" size="50"/></td>
</tr>
<tr>
<td>ZIP file - Student Portrait Photos: </td>
<td><input type="file" name="zip" size="50"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Save">
</td>
</tr>
</table>
</form>
</body>
</html>
This is the Java code:
@WebServlet(urlPatterns = {"/uploadServlet"})
@MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB
public class FileUploadDBServlet extends HttpServlet {
// database connection settings
private String dbURL = "jdbc:mysql://db_ip:3306/db_name";
private String dbUser = "root";
private String dbPass = "***";
private List<Part> uploadedParts;
private Part singleFile;
private Part uploadedZIP;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String message = null;
InputStream inputstream = null;//input stream of the single uploaded photo
ZipInputStream zipinputstream = null;
//get uploaded items
uploadedParts = (List<Part>) request.getParts();
singleFile = uploadedParts.get(0);//single image file
uploadedZIP = uploadedParts.get(1);//zip file
if(singleFile.getSize() != 0){
//print out file info for debugging
System.out.println(singleFile.getName());
System.out.println(singleFile.getSize());
System.out.println(singleFile.getContentType());
//get the file
inputstream = singleFile.getInputStream();
}
if(uploadedZIP.getSize() != 0){
//print out file info for debugging
System.out.println(uploadedZIP.getName());
//System.out.println(zipFile.getSize);
//System.out.println(zipFile.getContentType());
//get the file
ZipFile zipFile = new ZipFile((File) uploadedZIP);
Enumeration<?> enu = zipFile.entries();
while(enu.hasMoreElements()){
ZipEntry zipEntry = (ZipEntry) enu.nextElement();
String name = zipEntry.getName();
long size = zipEntry.getSize();
long compressedSize = zipEntry.getCompressedSize();
System.out.printf("name: %-20s | size: %6d | compressed size: %6d\n",
name, size, compressedSize);
}
}
Connection con = null;
try{
//connect to the database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
con = DriverManager.getConnection(dbURL, dbUser, dbPass);
//construct sql statement
String sql = "INSERT INTO StudentInfo (img, student) values (?,?)";
PreparedStatement statement = con.prepareStatement(sql);
if(inputstream != null){
//fetches input stream of the upload file for the blob column
statement.setBlob(1, inputstream);
statement.setString(2, getFileName(singleFile));
}
else{message = "No file was uploaded";}
//sends the statement to the database server
int row = statement.executeUpdate();
if(row > 0)
message = "Student image uploaded successfully";
else
message = "the image was not inserted into the database";
}catch(SQLException ex){
message = "Something went wrong!! see below \n" + ex.getMessage() + getFileName(singleFile);
}finally{
if(con != null){
//close db connection
try{
con.close();
}catch(SQLException ex){
ex.printStackTrace();
}
}
//sets the message in request scope
request.setAttribute("Message", message);
// forwards to the message page
getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
}
}