【问题标题】:JSP how to get the fully qualified name of a file in JSPJSP如何在JSP中获取文件的完全限定名
【发布时间】:2013-06-19 16:51:57
【问题描述】:

我正在尝试使用

在 JSP 中上传文件
<form action="EdgeWarUpload" method="post"
                    enctype="multipart/form-data">
  <input type="file" name="file" size="50" />
  <br />
      <input type="submit" value="Upload File" />

其中 EdgeWarUpload 是一个 servlet。用户正在浏览并选择要上传的文件。我希望 servlet EdgeWarUpload 中带有文件名(路径名 + 文件名)的完全限定路径来创建一个 BufferedInputStream。但我无法获得请查收并回复。

【问题讨论】:

  • 首先,我认为在服务器端没有使用来自用户的完全限定文件名,例如“C:\users\example\example.jpg”。其次,我认为这无论如何都不会暴露,因为它可能会泄露一些敏感信息作为副作用。
  • 我不相信你真的能得到这些信息......你为什么想要它?

标签: java jsp


【解决方案1】:
<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>  

试试这个

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

你真的不需要完全合格的路径

【讨论】:

  • 潜在的文件描述符泄漏!您的 .close() 应该在 finally 块中
  • 嗨@shreyansh jogi,我试过这个上传战争文件。但是上传后战争已经损坏了。请检查。
【解决方案2】:

几乎不可能。

浏览器不会发送完整路径,因为它被认为存在安全风险,因为它可能会透露有关客户端系统的信息,因此大多数现代浏览器都支持。

我猜在服务器端没有用。只需使用filename

【讨论】:

    【解决方案3】:

    这里你可以在 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();
    

    【讨论】:

      猜你喜欢
      • 2010-10-26
      • 2011-02-05
      • 1970-01-01
      • 2015-03-11
      • 2014-05-10
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 2011-04-28
      相关资源
      最近更新 更多