简体   繁体   English

使用 servlet 上传后图像文件损坏

[英]Image file is corrupted after uploading using servlet

I am try uploading the image using the below servlet But after upload I am able save the file but I am not able to open I checked the file is corrupted.我正在尝试使用下面的 servlet 上传图像但是上传后我可以保存文件但我无法打开我检查了文件已损坏。

Instead of using the annotation I have describe multipart-config in web.xml.我没有使用注释,而是在 web.xml 中描述了 multipart-config。 I this code I am trying to get the image file I send the data using AJAX.在这段代码中,我试图获取使用 AJAX 发送数据的图像文件。 Then I am redirected to Register servlet there I am using InputStream class to handle data.然后我被重定向到注册 servlet,我正在使用 InputStream class 来处理数据。 After this I creating the file and upload this Inputdata to file in some directory on server.在此之后,我创建文件并将此 Inputdata 上传到服务器上某个目录中的文件。

public class Register extends HttpServlet{
    @Override 
    protected void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String email=req.getParameter("email");
        Part part = req.getPart("image");
        String filename = part.getSubmittedFileName();
        InputStream is = req.getInputStream();
        byte[] data = new byte[is.available()];
        String path = "D:\\FullstackWeb\\images\\icon\\"+filename;
        System.out.println(path);
        FileOutputStream fos=new FileOutputStream(path);
        fos.write(data);
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://dns1.nishchay.com:3306/register","demouser","123Nbr@");
            String query = "Insert INTO register.signup(username,email,userpassword,filename) values(? ,?, ?,?)";
            PreparedStatement pstmt= conn.prepareStatement(query);
            pstmt.setString(1, username);
            pstmt.setString(2, email);
            pstmt.setString(3, password);
            pstmt.setString(4, path);       
            pstmt.executeUpdate();      
            conn.close();
        }catch(Exception e) {
            out.println("<h1>Issue is occured</h1>");       
        }       
    }
}```


You are not reading in the image data:您没有读取图像数据:

    InputStream is = req.getInputStream();
    byte[] data = new byte[is.available()];
    String path = "D:\\FullstackWeb\\images\\icon\\"+filename;
    System.out.println(path);
    FileOutputStream fos=new FileOutputStream(path);
    fos.write(data);

does not contain any is.read() call and it doesn't close the FileOutputStream .不包含任何is.read()调用,也不会关闭FileOutputStream

In addition to that your allocated buffer is to small for most images.除此之外,您分配的缓冲区对于大多数图像来说都很小。 The JavaDoc for InputStream.available() states InputStream.available()状态的 JavaDoc

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking返回可以从此输入 stream 无阻塞地读取(或跳过)的字节数的估计值

To completely read the image data you could simply replace the above code with要完全读取图像数据,您只需将上面的代码替换为

Files.copy(is, Paths.get("D:\\FullstackWeb\\images\\icon\\"+filename));

but with a big caveat : since the file name is supplied by the user of your service this opens your code to security problems.但有一个很大的警告:由于文件名是由您的服务用户提供的,这会使您的代码面临安全问题。

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

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