0

我正在尝试使用在 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。但我无法得到它。请检查并回复。

4

3 回答 3

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

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

于 2013-06-19T08:35:44.027 回答
0

在这里,您可以在 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();
于 2013-06-19T08:44:37.647 回答
0

大多不可能。

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

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

于 2013-06-19T08:37:39.813 回答