简体   繁体   English

JSP如何在JSP中获取文件的标准名称

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

I am trying to upload a file in JSP using the 我正在尝试使用JSP在JSP中上传文件

<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. EdgeWarUpload是Servlet,用户正在浏览并选择要上传的文件。我希望Servlet EdgeWarUpload中具有文件名(路径名+文件名)的完全限定路径创建BufferedInputStream,但是我无法获取它。检查并回复。

<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 您真的不需要完全限定的路径

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 . 只需使用filename

here you can use this code in servlet for the file name: 在这里,您可以在servlet中使用以下代码作为文件名:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM