繁体   English   中英

如何使用Java上传文件?

[英]How to upload file using java ?

您好,我试图使用Java文件上传文件。但是我没有得到它。我得到的文件大小为0,我在这里提供我的Java代码。 告诉我为什么我不能上传到特定的文件夹。 我想将我的文件存储在特定的文件夹中。 我正在尝试获取文件大小,文件名,但我在错误的地方输入了空值,请告诉我。

public void updateTesti(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException, PortletException
{
    //image upload logic
    String folder_for_upload =(getPortletContext().getRealPath("/"));
    //String folder=actionRequest.getParameter("uploadfolder");
    realPath=getPortletContext().getRealPath("/");  

    logger.info("RealPath is" + realPath);
    logger.info("Folder is :" + folder_for_upload);
    try
    {
        logger.info("Admin is try to upload");

        UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);

        if (uploadRequest.getSize("fileName") == 0) {
            SessionErrors.add(actionRequest, "error");
        }
        String sourceFileName = uploadRequest.getFileName("fileName");
        File uploadedFile = uploadRequest.getFile("fileName");
        System.out.println("Size of uploaded file: " + uploadRequest.getSize("fileName"));

        logger.info("Uploded file name is: " + uploadRequest.getFileName("fileName"));                  
        String destiFolder=("/home/ubuntu/liferay/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/webapps/imageUpload-portlet/image");
        String newsourcefilename = (uploadRequest.getFileName("fileName"));
        File  newFile = new File(destiFolder +"/"+ newsourcefilename);

        logger.info("New file name: " + newFile.getName());
        logger.info("New file path: " + newFile.getPath());

        InputStream in = new BufferedInputStream(uploadRequest.getFileAsStream("fileName"));

        FileInputStream fis = new FileInputStream(uploadedFile);
        FileOutputStream fos = new FileOutputStream(newFile);
        byte[] bytes_ = FileUtil.getBytes(in);
        int i = fis.read(bytes_);
        while (i != -1) {
            fos.write(bytes_, 0, i);
            i = fis.read(bytes_);
        }
        fis.close();
        fos.close();
        Float size = (float) newFile.length();

        System.out.println("file size bytes:" + size);
        System.out.println("file size Mb:" + size / 1048576);

        logger.info("File created: " + newFile.getName());
        SessionMessages.add(actionRequest, "success");

    }
    catch (FileNotFoundException e)
    {
        System.out.println("File Not Found.");
        e.printStackTrace();
        SessionMessages.add(actionRequest, "error");
    }
    catch (NullPointerException e)
    {
        System.out.println("File Not Found");
        e.printStackTrace();
        SessionMessages.add(actionRequest, "error");
    }
    catch (IOException e1)
    {
        System.out.println("Error Reading The File.");
        SessionMessages.add(actionRequest, "error");
        e1.printStackTrace();
    }
}

您需要执行此操作以上传小于1kb的小文件

File f2 = uploadRequest.getFile("fileupload", true);

它们仅存储在内存中。 如果我得到一个空指针,或者在我的原始文件(f1.length)== 0的情况下,我在catch语句中有它

我已经执行了您的代码。它按预期运行。您的jsp页面可能有问题。我不确定,但是可能是您的name属性与您在processAction中使用的属性不同(假设您在使用portlet)。参数区分大小写,因此请再次检查。

您将在下面的链接中找到更多信息。文件上传中有很好的解释。

http://www.codeyouneed.com/liferay-portlet-file-upload-tutorial/

我检查了一个文件上传代码,当我在本地系统中实现该代码时,portlet将我上传的文件保存在tomcat/webbapp/abc_portlet_project位置中,我不了解的是在哪里找到了portlet

字符串文件夹= getInitParameter(“ uploadFolder”);

  String realPath = getPortletContext().getRealPath("/"); System.out.println("RealPath" + realPath +"\\\\" + folder); try { 

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest); System.out.println(“ Size:” + uploadRequest.getSize(“ fileName”)));

如果(uploadRequest.getSize(“ fileName”)== 0){SessionErrors.add(actionRequest,“ error”);}

字符串sourceFileName = uploadRequest.getFileName(“ fileName”); 文件file = uploadRequest.getFile(“ fileName”);

System.out.println(“ Nome file:” + uploadRequest.getFileName(“ fileName”)); 文件newFolder = null; newFolder =新文件(realPath +“ \\” +文件夹); if(!newFolder.exists()){newFolder.mkdir(); }文件newfile = null; newfile =新文件(newFolder.getAbsoluteFile()+“ \\” + sourceFileName); System.out.println(“新文件名:” + newfile.getName()); System.out.println(“新文件路径:” + newfile.getPath());

InputStream in = new BufferedInputStream(uploadRequest.getFileAsStream(“ fileName”)); FileInputStream fis =新的FileInputStream(file); FileOutputStream fos = new FileOutputStream(newfile);

暂无
暂无

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

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