2015-05-29 84 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'位置。 –

相关问题