How to get the absolute path in Java

Uday Konduru

Currently i am uploading one image from browser.Now in back end there is java code there i am retrieving this images and converting into byte array and storing into database this part working actually fine

The Code goes like this:

String fromLocal = "D:/123.jpg";
        File file = new File(fromLocal);
        InputStream inputStream = null;
        byte[] bFile= null;
        byte[] imageData = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(file));
            ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
            bFile = new byte[8192];
            int count;
            while((count = inputStream.read(bFile))> 0){
                baos.write(bFile, 0, count);
            }
            bFile = baos.toByteArray();
            if(bFile.length > 0){
                imageData = bFile;
            }
            baos.flush();
            inputStream.close();
        } catch (Exception ioe) {
            //throw ioe;
        }

Problem is whenever i am trying to hardcode the image path (like here D:/123.jpg) it is working absolutely fine. Now its user dependent & client dependent he can able to load the image from any drive & from any directory.I dont have privilege of using servlet. My query is :

1.Now,if i am trying to upload the same image from D:/123.jpg from browser i am getting only 123.jpg not the absolute path like D:/123.jpg. Because of the same now not able to process the image.

2.How to know that particular path from where user was trying to upload the image.(lets say user uploading image from C:/images/123.jpg),so how to get this absolute path.

I tried my best to detailed my problem,let me know if something is not clear i will try to explain in some other way.

T.J. Crowder

If the user is uploading a file to your servlet, the file is contained in the request body, it's not on any path on the server. The path on the end user's client machine is irrelevant (and you can't access it anyway, not even client-side).

There's a Java EE 6 tutoral on file upload here: http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html

Fundamentally (from that example), if the field used for upload has the name file, you do this in your servlet:

final Part filePart = request.getPart("file");
InputStream filecontent = null;

and then later within a try/catch:

filecontent = filePart.getInputStream();

...and use the data from the stream.

Here's the source from the tutorial above (in case it's inaccessible when someone's reading this later). In this case, it's writing the file out to a file server-side, but in your case of course you would populate your imageData (which I take it you then put in a DB) instead.

@WebServlet(name = "FileUploadServlet", urlPatterns = {"/upload"})
@MultipartConfig
public class FileUploadServlet extends HttpServlet {

    private final static Logger LOGGER = 
            Logger.getLogger(FileUploadServlet.class.getCanonicalName());

    protected void processRequest(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        // Create path components to save the file
        final String path = request.getParameter("destination");
        final Part filePart = request.getPart("file");
        final String fileName = getFileName(filePart);

        OutputStream out = null;
        InputStream filecontent = null;
        final PrintWriter writer = response.getWriter();

        try {
            out = new FileOutputStream(new File(path + File.separator
                    + fileName));
            filecontent = filePart.getInputStream();

            int read = 0;
            final byte[] bytes = new byte[1024];

            while ((read = filecontent.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            writer.println("New file " + fileName + " created at " + path);
            LOGGER.log(Level.INFO, "File{0}being uploaded to {1}", 
                    new Object[]{fileName, path});
        } catch (FileNotFoundException fne) {
            writer.println("You either did not specify a file to upload or are "
                    + "trying to upload a file to a protected or nonexistent "
                    + "location.");
            writer.println("<br/> ERROR: " + fne.getMessage());

            LOGGER.log(Level.SEVERE, "Problems during file upload. Error: {0}", 
                    new Object[]{fne.getMessage()});
        } finally {
            if (out != null) {
                out.close();
            }
            if (filecontent != null) {
                filecontent.close();
            }
            if (writer != null) {
                writer.close();
            }
        }
    }

    private String getFileName(final Part part) {
        final String partHeader = part.getHeader("content-disposition");
        LOGGER.log(Level.INFO, "Part Header = {0}", partHeader);
        for (String content : part.getHeader("content-disposition").split(";")) {
            if (content.trim().startsWith("filename")) {
                return content.substring(
                        content.indexOf('=') + 1).trim().replace("\"", "");
            }
        }
        return null;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the absolute path for the resources directory in Java?

From Dev

How to get the file absolute path in JAVA project

From Dev

How to get Java File absolute path from InputStream?

From Dev

How to get absolute path with proper character encoding in Java?

From Dev

Get Absolute java.nio.Path

From Dev

Get absolute path of "web content" folder in Java

From Dev

How can I get the absolute path to the gulpfile?

From Java

How to get an absolute file path in Python

From Dev

How to get absolute path on a zsh prompt?

From Dev

How to get absolute path of a view in android?

From Dev

How to get absolute path from FileDialog?

From Dev

Python / How to get logger with absolute path?

From Dev

How to get absolute path to a file packaged with the apk?

From Dev

Java nio: How to add extension to an absolute path?

From Dev

How can I get relative path from absolute http paths in Java

From Dev

How to get absolute path of a pathlib.Path object?

From Dev

Get absolute path to File

From Dev

How to get the absolute path of a file in a Symfony2 controller?

From Java

How to get absolute path to file in /resources folder of your project

From Dev

MSBuild: How to get the relative path to a file, given an absolute one?

From Dev

How to get the absolute path of a file in Symfony2?

From Dev

How do I get the absolute path of an INI file with Delphi?

From Dev

How to get the correct absolute file path after maven project is deployed

From Dev

How do I get the absolute path of the current page in Angular 2?

From Dev

How to get uploaded absolute s3 image path in Django

From Dev

How to get Absolute path from android directory URI

From Dev

Python: How to get absolute path of file in imported module

From Dev

How to get the absolute path of a file stored in Sdcard - Android

From Dev

How to get the absolute path of a file in a Symfony2 controller?

Related Related

  1. 1

    How to get the absolute path for the resources directory in Java?

  2. 2

    How to get the file absolute path in JAVA project

  3. 3

    How to get Java File absolute path from InputStream?

  4. 4

    How to get absolute path with proper character encoding in Java?

  5. 5

    Get Absolute java.nio.Path

  6. 6

    Get absolute path of "web content" folder in Java

  7. 7

    How can I get the absolute path to the gulpfile?

  8. 8

    How to get an absolute file path in Python

  9. 9

    How to get absolute path on a zsh prompt?

  10. 10

    How to get absolute path of a view in android?

  11. 11

    How to get absolute path from FileDialog?

  12. 12

    Python / How to get logger with absolute path?

  13. 13

    How to get absolute path to a file packaged with the apk?

  14. 14

    Java nio: How to add extension to an absolute path?

  15. 15

    How can I get relative path from absolute http paths in Java

  16. 16

    How to get absolute path of a pathlib.Path object?

  17. 17

    Get absolute path to File

  18. 18

    How to get the absolute path of a file in a Symfony2 controller?

  19. 19

    How to get absolute path to file in /resources folder of your project

  20. 20

    MSBuild: How to get the relative path to a file, given an absolute one?

  21. 21

    How to get the absolute path of a file in Symfony2?

  22. 22

    How do I get the absolute path of an INI file with Delphi?

  23. 23

    How to get the correct absolute file path after maven project is deployed

  24. 24

    How do I get the absolute path of the current page in Angular 2?

  25. 25

    How to get uploaded absolute s3 image path in Django

  26. 26

    How to get Absolute path from android directory URI

  27. 27

    Python: How to get absolute path of file in imported module

  28. 28

    How to get the absolute path of a file stored in Sdcard - Android

  29. 29

    How to get the absolute path of a file in a Symfony2 controller?

HotTag

Archive