1

这是我的控制台错误,当我尝试上传图像时,它会显示此错误,请帮助我解决这种情况我想在第二页上渲染图像并显示其缩略图,并且在运行时它将创建文件夹结构用户和子文件夹图像

 hhhhhhhhhhhhhhhhhhhhhhhh
    hiiiiiiiiiiiiiiiiiiiiiiiii
    [name=Desert.jpg, StoreLocation=D:\testing\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\uploadimg\upload__26aeaf5b_1425b674704__8000_00000000.tmp, size=845941bytes, isFormField=false, FieldName=file]
    hello nhi aaya
    mein aaya bahar le k
    yaha pe aaja 
    **Desert.jpg
    Desert.jpg
    java.io.FileNotFoundException: D:\testing\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\uploadimg\users\images\Desert.jpg (The system cannot find the path specified)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at org.apache.commons.fileupload.disk.DiskFileItem.write(DiskFileItem.java:439)
        at com.vaibhav.upload.doPost(upload.java:80)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 

upload.java

    package com.vaibhav;

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Iterator;
    import java.util.List;
    import javax.imageio.ImageIO;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.FileItemFactory;
    import org.apache.commons.fileupload.FileUploadException;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    import org.imgscalr.Scalr;

    public class upload extends HttpServlet {
        private boolean isMultipart;
        String ofileName;
        String fieldName;
        String contentType;
        File usersDir = null;
        private static final long serialVersionUID = 1L;

        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws IOException, ServletException {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();

            isMultipart = ServletFileUpload.isMultipartContent(request);
            System.out.println("hhhhhhhhhhhhhhhhhhhhhhhh");
            if (!isMultipart) {
            } else {

                File fobj = new File(getServletContext().getRealPath("\\"));
                FileItemFactory factory = new DiskFileItemFactory(1024 * 5, fobj);
                ServletFileUpload upload = new ServletFileUpload(factory);
                List items = null;
                System.out.println("hiiiiiiiiiiiiiiiiiiiiiiiii");
                try {
                    items = upload.parseRequest(request);
                } catch (FileUploadException e) {
                    e.printStackTrace();
                }
                System.out.println(items);
                Iterator itr1 = items.iterator();
                System.out.println("hello nhi aaya");
                FileItem fileItem = null;

                while (itr1.hasNext()) {
                    System.out.println("mein aaya bahar le k");
                    FileItem item = (FileItem) itr1.next();
                    if (item.isFormField()) {
                        item.getFieldName();
                        item.getString();
                    } else {
                        System.out.println("yaha pe aaja ");

                        fileItem = item;
                        String finalName;
                        int namestart;
                        fieldName = item.getFieldName();System.out.println("**"+item.getName());
                        ofileName = item.getName();
                        contentType = fileItem.getContentType();
                        System.out.println(ofileName);
                        namestart = ofileName.lastIndexOf("//");
                        if (namestart > -1) {
                            finalName = ofileName.substring(namestart);
                        } else {
                            finalName = ofileName;  // there's nothing to substring
                        }
                        usersDir = new File(getServletContext().getRealPath("\\")
                                + "\\users\\" + "\\images\\" + finalName);
                        try {
                            fileItem.write(usersDir);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        String imgExt = usersDir.getAbsolutePath().substring(
                                usersDir.getAbsolutePath().lastIndexOf(".") + 1);
                        BufferedImage bimg = new BufferedImage(100, 100,
                                BufferedImage.TYPE_INT_RGB);
                        bimg.createGraphics().drawImage(
                                ImageIO.read(usersDir).getScaledInstance(170, 150,
                                        java.awt.Image.SCALE_SMOOTH), 0, 0, null);
                        BufferedImage bimg2 = Scalr.resize(bimg, 150);
                        ImageIO.write(bimg2, imgExt, new File(getServletContext()
                                .getRealPath("\\")
                                + "\\users\\"
                                + "\\images\\thumb_" + finalName));
                    }
                    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/webpages/home.jsp");
                    dispatcher.forward(request, response);
                }
            }
        }
    }

..................................................... ..................... 索引.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>file upload</title>
</head>
<body>
    <form action="upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file" size="50"><br/> <input
            type="submit" value="uploadfile">
    </form>
</body>
</html>
4

1 回答 1

0

您的应用程序无法找到文件夹“用户/图像”。

要么转到“index.jsp”所在的项目,然后(在同一级别)创建文件夹“users”并在其下再创建一个文件夹“images”。(换句话说,在“uploadimg”文件夹中)

要不然

不要在代码中使用 "\users\" + "\images\""。

于 2013-11-16T07:25:59.543 回答