加载图像文件时出错

时间:2011-04-25 07:38:43

标签: java

uploadimage.html

         <html>

   <head><title>Image Upload</title></head>

      <body>
<form action="UploadImage" method="post" enctype="multipart/form-data" name="productForm" id="productForm"><br><br>
    <table width="400px" align="center" border=0 style="background-color:ffeeff;">
        <tr>
            <td align="center" colspan=2 style="font-weight:bold;font-size:20pt;">Image Details</td>
        </tr>

        <tr>
            <td align="center" colspan=2>&nbsp;</td>
        </tr>

        <tr>
            <td>Image Link: </td>
            <td>
                <input type="file" name="file" id="file">
            <td>
        </tr>

        <tr>
            <td></td>
            <td><input type="submit" name="Submit" value="Submit"></td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;</td>
        </tr>

    </table>
</form>
</body>

  </html>

UploadImage.java

   public class UploadImage extends HttpServlet{
          @Override
           public void doPost(HttpServletRequest request, 
          HttpServletResponse  response) throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    out.println("request: "+request);
    if (!isMultipart) {
        out.println("File Not Uploaded");
    } else {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List items = null;

        try {
            items = upload.parseRequest(request);
            out.println("items: "+items);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        Iterator itr = items.iterator();
        while (itr.hasNext()) {
            FileItem item = (FileItem) itr.next();
            if (item.isFormField()){
                String name = item.getFieldName();
                System.out.println("name: "+name);
                String value = item.getString();
                System.out.println("value: "+value);
            } else {
                try {
                    String itemName = item.getName();
                    Random generator = new Random();
                    int r = Math.abs(generator.nextInt());

                    String reg = "[.*]";
                    String replacingtext = "";
                    out.println("Text before replacing is:-" + itemName);
                    Pattern pattern = Pattern.compile(reg);
                    Matcher matcher = pattern.matcher(itemName);
                    StringBuffer buffer = new StringBuffer();

                    while (matcher.find()) {
                        matcher.appendReplacement(buffer, replacingtext);
                    }
                    int IndexOf = itemName.indexOf(".");
                    String domainName = itemName.substring(IndexOf);
                    out.println("domainName: "+domainName);

                    String finalimage = buffer.toString()+"_"+r+domainName;
                    out.println("Final Image==="+finalimage);

                    File savedFile = new File("C:\\Users\\Nirmala\\Documents\\NetBeansProjects\\tabswitch\\web\\"+"images\\"+finalimage);
                    item.write(savedFile);
                    out.println("<html>");
                    out.println("<body>");
                    out.println("<table><tr><td>");
                    out.println("<img src=images/"+finalimage+">");
                    out.println("</td></tr></table>");

                    /*Connection conn = null;
                    String url = "jdbc:mysql://localhost:3306/";
                    String dbName = "test";
                    String driver = "com.mysql.jdbc.Driver";
                    String username = "root";
                    String userPassword = "root";
                    String strQuery = null;
                    String strQuery1 = null;
                    String imgLen="";

                    try {
                        System.out.println("itemName::::: "+itemName);
                        Class.forName(driver).newInstance();
                        conn = DriverManager.getConnection(url+dbName,username,userPassword);
                        Statement st = conn.createStatement();
                        strQuery = "insert into testimage set image='"+finalimage+"'";
                        int rs = st.executeUpdate(strQuery);
                        System.out.println("Query Executed Successfully++++++++++++++");
                        out.println("image inserted successfully");
                        out.println("</body>");
                        out.println("</html>");
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    } finally {
                        conn.close();
                    }   */
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

}

但我观察到一件奇怪的事情......图像没有显示出来......图像在servlet中被读取之后就被创建了......请帮助我......

输出是...... 替换前的文本是:-LogoBMP.bmp domainName:.bmp最终图像=== LogoBMP_726573791.bmp

1 个答案:

答案 0 :(得分:0)

您可能打算将调试消息发送到标准输出标准错误,但是您以某种方式将其与 PrintWriter 混合在一起用于HTML标记。

因此,部分out.println语句应更改为System.out.println

...

PrintWriter out = response.getWriter();

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
System.out.println("request: "+request);
if (!isMultipart) {
    System.out.println("File Not Uploaded");
} else {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List items = null;

    try {
        items = upload.parseRequest(request);
        System.out.println("items: "+items);
    } catch (FileUploadException e) {
        e.printStackTrace();
    }

    ...