user1281029
user1281029

Reputation: 1533

JSP how to get the fully qualified name of a file in JSP

I am trying to upload a file in JSP using the

<form action="EdgeWarUpload" method="post"
                    enctype="multipart/form-data">
  <input type="file" name="file" size="50" />
  <br />
      <input type="submit" value="Upload File" />

where EdgeWarUpload is a servlet.User is browsing and selecting a file to upload.I want the fully qualified path with filename (path name + file name) in the servlet EdgeWarUpload to create a BufferedInputStream.But I am not able to get it.Kindly check and reply.

Upvotes: 0

Views: 1360

Answers (3)

anonymous
anonymous

Reputation: 244

here you can use this code in servlet for the file name:

DataInputStream in = new DataInputStream(request.getInputStream());
        int formDataLength = request.getContentLength();
        byte dataBytes[] = new byte[formDataLength];
        int byteRead = 0;
        int totalBytesRead = 0;

        while (totalBytesRead < formDataLength) {
            byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
            totalBytesRead += byteRead;
        }
        String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
            saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
            saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
            System.out.println("filename3: "+ saveFile);    // name of the file

int lastIndex = contentType.lastIndexOf("=");
            String boundary = contentType.substring(lastIndex + 1, contentType.length());

            int pos;
            pos = file.indexOf("filename=\"");
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            int boundaryLocation = file.indexOf(boundary, pos) - 4;
            int startPos = ((file.substring(0, pos)).getBytes()).length;
            int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
            FileOutputStream fileOut = new FileOutputStream(saveFile);
            fileOut.write(dataBytes, startPos, (endPos - startPos));
            fileOut.flush();
            fileOut.close();

            FileInputStream fis = new FileInputStream(saveFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum);
            }
            byte[] bytes = bos.toByteArray();


            bos.close();
fis.close();
in.close();

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

Mostly Not Possible.

Browsers wont send the full path because its considered a security risk because it might tell thing about the client system, so most modern browsers support that.

on server side no use of that I guess . just use filename.

Upvotes: 0

shreyansh jogi
shreyansh jogi

Reputation: 2102

<html>  
    <header></header>  
    <body>  
        <form method="POST" action="upload.do">  
            escolha o arquivo para fazer upload:   
            <input type="file" name="ctrupload"><br>  
            <input type="submit" name="submit" value="enviar...">  
        </form>  
    </body>  
</html>  

try this

public class Uploader extends HttpServlet {  
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        ServletInputStream sis = request.getInputStream();  

        byte[] b = new byte[request.getContentLength()];  

        System.out.println(request.getContentLength());  

        sis.read(b,0,b.length);  

        FileOutputStream fos = new FileOutputStream("Teste.jpg");  
        fos.write(b);  

        sis.close();  
        fos.flush();  
        fos.close();          
    }  
}  

you really dont required fully qualified path

Upvotes: 1

Related Questions