当我上传文件我得到"文件夹"值null

时间:2015-05-29 07:26:57

标签: liferay liferay-6

我已经通过代码在本地系统中上传文件。我有文件夹值null,并在上传文件后,它存储了为什么我在位置Liferay/tomcat/Root/webapps/My portletname/提到了空文件夹? 我的view.jsp就在这里。

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
 <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
 <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>
 <%@ page import="com.liferay.portal.kernel.util.ParamUtil"%>
 <%@ page import="com.liferay.portal.kernel.util.Validator"%>
 <%@ page import="javax.portlet.PortletPreferences"%>
 <%@ page import="com.liferay.util.PwdGenerator"%>
 <portlet:defineObjects />
<%
String uploadProgressId = PwdGenerator.getPassword(PwdGenerator.KEY3, 4);
    PortletPreferences prefs = renderRequest.getPreferences();

%>

<portlet:actionURL var="editCaseURL" name="uploadCase">
<portlet:param name="jspPage" value="/edit.jsp" />
</portlet:actionURL>

<liferay-ui:error key="error"
   message="Sorry, an error prevented the upload. Please try again." />
<liferay-ui:upload-progress
    id="<%= uploadProgressId %>"
    message="uploading"
    redirect="<%= editCaseURL %>"
  />

<aui:form action="<%= editCaseURL %>" enctype="multipart/form-data" method="post" >
<select>
  <option value="store">Store</option>
  <option value="floor">Floor</option>
  <option value="department">Department</option>
</select> 
<aui:input type="file" name="fileName" size="75"/>
<input type="submit" value="<liferay-ui:message key="upload" />" onClick="<%= uploadProgressId %>.startProgress(); return true;"/>
<!--  aui:button type="submit" value="Save" /-->
</aui:form>

<br />
<portlet:renderURL var="viewCaseURL">
    <portlet:param name="jspPage" value="/view2.jsp" />
</portlet:renderURL>

<aui:button onClick="<%=viewCaseURL%>" value="view Uploaded Doc" />

我的控制器在这里:

package com.upload.doc;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;


  public class DocUpload extends MVCPortlet {
            public void uploadCase(ActionRequest actionRequest, ActionResponse actionRresponse) throws PortletException,IOException {

                   String folder = 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"));

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

String sourceFileName = uploadRequest.getFileName("fileName");
File file = uploadRequest.getFile("fileName");

System.out.println("Name file:" + uploadRequest.getFileName("fileName"));
File newFolder = null;
newFolder = new File(realPath +"\\" + folder);
if(!newFolder.exists()){
newFolder.mkdir();
}
             File newfile = null;
               newfile = new File(newFolder.getAbsoluteFile()+"\\"+sourceFileName);
               System.out.println("New file name: " + newfile.getName());
               System.out.println("New file path: " + newfile.getPath());

            InputStream in = new BufferedInputStream(uploadRequest.getFileAsStream("fileName"));
            FileInputStream fis = new FileInputStream(file);
            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);

          System.out.println("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();
}
}
} 

我的portlet.xml在这里:

<?xml version="1.0"?>

<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">
    <portlet>
        <portlet-name>docupload</portlet-name>
        <display-name>Docupload</display-name>
        <portlet-class>com.upload.doc.DocUpload</portlet-class>
        <init-param>
            <name>view-template</name>
            <value>/view.jsp</value>
        </init-param>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
        </supports>
        <portlet-info>
            <title>Docupload</title>
            <short-title>Docupload</short-title>
            <keywords>Docupload</keywords>
        </portlet-info>
        <security-role-ref>
            <role-name>administrator</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>guest</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>power-user</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>user</role-name>
        </security-role-ref>
    </portlet>
</portlet-app>

1 个答案:

答案 0 :(得分:1)

让我回答你的问题,不回答它,但指出你所犯的潜在设计错误:

最佳做法是将任何内容上传到您的webapplication文件夹中。这有多种原因:

  • 你永远无法知道究竟是什么部署
  • 当应用程序不断变化时,灾难恢复会更加困难
  • 您可以将应用程序服务器配置为甚至不提供文件系统中的文件,而只是来自压缩的war文件
  • 最佳做法是让webapp对应用程序服务器进行写保护,这样任何人都无法通过将任意内容上传到您的Web应用程序来攻击服务器
  • 假设有人将fileexplorer.jsp上传到您的appserver,然后通过浏览器访问该文件。或者,有人注入/WEB-INF/lib/attacking-code.jar

所有这些原因促使我建议您不要寻找解决您所说的问题的方法。您有一个潜在的问题需要解决。

在应用程序服务器之外找到存储数据的位置。重新设计您的应用程序,使其不依赖于自己webcontext中的上传文件。也许所述的上传问题就会消失。如果没有,请再次询问,然后再使用新的上下文。