2015-05-29 85 views
0

我已經通過代碼上傳文件在我們的本地系統。我得到的文件夾值爲空,上傳文件後,它存儲了爲什麼我在位置Liferay/tomcat/Root/webapps/My portletname/引用它與空文件夾? 我的view.jsp在這裏。當我上傳文件我得到「文件夾」值null

<%@ 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> 
+0

crossreferencing https://www.liferay.com/community/forums/-/message_boards/message/54565674 –

回答

1

讓我不回答就回答你的問題,而是指出潛在的你所做的設計錯誤:

最好的做法是不是將任何內容上傳到您的web應用程序的文件夾。這是有多種原因:

  • 你永遠無法知道究竟是部署
  • 災難恢復是很難當應用程序不斷變化
  • 您可以配置應用程序服務器,甚至不從文件系統提供文件,但只是從壓縮文件中提取文件
  • 假設某人上傳fileexplorer.jsp到您的應用程序服務器,然後通過瀏覽器訪問該文件。或者,有人注入/WEB-INF/lib/attacking-code.jar

所有這些原因導致我建議您不要尋找解決您所說的問題的解決方案。你有一個你想解決的潛在問題。

查找在應用程序服務器外部存儲數據的地方。重新設計您的應用程序,使其不依賴於自己的webcontext中上傳的文件。也許所述的上傳問題隨即消失。如果不是,再問一遍,然後用新的上下文。

+0

謝謝你的回覆,因爲你列出了很多的缺點,這是真正的關注我作爲好。我可以通過liferay-ext.properties更改屬性以上載文件。但它會上傳整個liferay實例上傳的所有文件,我正在尋找答案,我該如何爲單站portlet做到這一點。 –

+0

是不是意味着上傳到你的portlet的目錄而不是Liferay的?如果是這樣:這沒有什麼區別。否則:我不明白你想要什麼,否則。您可以找到無數文章來了解如何實現文件上傳portlet。我可能已經投了票關閉一些重疊在stackoverflow上,但他們幾乎無處不在。只*不*上傳到部署到Web服務器的應用程序目錄。不要,不要,不要! –

+0

我不打算這麼做,因爲你可以看到'getPortletContext()。getRealPath'正在將文件上傳到liferay的臨時文件夾中,但是我希望它將它上傳到我自定義的'/ home/mydirectory'位置。 –

相關問題