0

您好,我正在尝试使用 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();
    }
}
4

3 回答 3

1

您需要这样做才能上传小于 1kb 的小文件

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

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

于 2017-04-07T17:46:17.440 回答
0

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

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

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

于 2015-05-28T20:16:11.250 回答
-1

我经历了一个文件上传代码,当我在我的本地系统中实现它时,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("大小:"+uploadRequest.getSize("fileName"));

if (uploadRequest.getSize("fileName")==0) {SessionErrors.add(actionRequest, "error");}

String sourceFileName = uploadRequest.getFileName("fileName"); 文件文件 = uploadRequest.getFile("fileName");

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

InputStream in = new BufferedInputStream(uploadRequest.getFileAsStream("fileName")); FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(newfile);

于 2015-05-28T14:17:51.377 回答