1
votes

I'm working with hibernate and tomcat. What I did is create a form which has a file input. In the servlet I want to cast that file to a byte[]. But when I want to create de File, there's no directory: it only saves the image's name, not the path. Here is the error:

java.io.FileNotFoundException: oldi.png (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:138) at control.servlets.AddGenericItem.doPost(AddGenericItem.java:46) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:724)

The HTML:

<tr>
    <td>
        <font class="text_title">Image: </font>
    </td>
    <td>
        <input name="image" type="file">
    </td>
</tr>

<tr>
    <td>
        <input type="submit" value="submit" name="ctl00$MainContent$RegisterUserWizard$CreateUserStepContainer$CreateButton"
               id="ctl00_MainContent_RegisterUserWizard_CreateUserStepContainer_CreateButton"
               style="border-width:0px;">
    </td>
</tr>

Servlet:

public class AddGenericItem extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
            throws ServletException, IOException {

//        HttpSession session = httpServletRequest.getSession();
        DatabaseManager databaseManager = new DatabaseManager();

        String name = httpServletRequest.getParameter("name");
        String description = httpServletRequest.getParameter("description");
        float price = Float.parseFloat(httpServletRequest.getParameter("price"));

        File image = new File(httpServletRequest.getParameter("image"));
        byte[] imageArray = new byte[(int) image.length()];
        try {
            FileInputStream fileInputStream = new FileInputStream(image);
            fileInputStream.read(imageArray);
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Admin admin = (Admin) httpServletRequest.getSession().getAttribute("user");
        Brand brand = admin.getBrand();

        GenericItem genericItem = new GenericItem(name, description, imageArray, price,brand);

        GenericItemDAO genericItemDAO = databaseManager.getGenericItemDAO();

        genericItemDAO.beginTransaction();
        genericItemDAO.save(genericItem);

        try {
            genericItemDAO.commitTransaction();
        } catch (DatabaseAccessFailException e) {
            e.getStackTrace();
        } finally {
            httpServletResponse.sendRedirect("/adminPages/items.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
            throws ServletException, IOException {

    }

    @Override
    protected void doDelete(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
            throws ServletException, IOException {

    }

}
1
where is oldi.png present in the project?Braj
are you trying to upload image?bNd
oldi.png is not present in the project, I'm trying to upload it from the Desktop.user3808750

1 Answers

0
votes

I hope you are working on File Upload.

The problem is at below line:

FileInputStream fileInputStream = new FileInputStream(image);

Servlet looks for the file at the server to read it but at that time it will not be there.


Use Commons Fileupload library provided by Apache that makes it easy to add robust, high-performance, file upload capability to your servlets and web applications.

Find complete code here and here and here