如何在相对目录中上传文件 [英] How to upload file in relative directory

查看:88
本文介绍了如何在相对目录中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个应用程序,可以在其中上传将保存在本地给定目录中的任何文件.我想修改它,我想为部门添加一个下拉列表(具有多个选项,例如,楼层,商店,区域).也就是说,如果我们要上传存储"文件夹中的文件,则可以选择存储"选项,文件将上传到存储"文件夹中. 地板"和部分"相同. 我只需要任何示例链接即可. 我已经在liferay中做到了.

I have made a application where we can upload any file which will save in our local given directory. I want to modify it, i want to add a drop down (with multiple option i.e floor, store, section) for department. i.e if we want to upload a file in 'Store' folder, we can choose 'Store' option and the file will uploaded to the 'Store' folder. Same for 'Floor' and 'Section'. I just need any example link for that. i have made it in liferay.

import org.apache.commons.io.FileUtils;


import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class UploadDirectory extends MVCPortlet {

    private final static int ONE_GB = 1073741824;

    private final static String baseDir = "/home/xxcompny/workspace";

    private final static String fileInputName = "fileupload";

    public void upload(ActionRequest request, ActionResponse response)
            throws Exception {

        UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);

        long sizeInBytes = uploadRequest.getSize(fileInputName);

        if (uploadRequest.getSize(fileInputName) == 0) {
            throw new Exception("Received file is 0 bytes!");
        }


        File uploadedFile = uploadRequest.getFile(fileInputName);

        String sourceFileName = uploadRequest.getFileName(fileInputName);



        File folder = new File(baseDir);



        if (folder.getUsableSpace() < ONE_GB) {
            throw new Exception("Out of disk space!");
        }


        File filePath = new File(folder.getAbsolutePath() + File.separator + sourceFileName);


        FileUtils.copyFile(uploadedFile, filePath);
    }

}

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"%>
<portlet:defineObjects />

<portlet:actionURL name="upload" var="uploadFileURL"></portlet:actionURL>

<aui:form action="<%= uploadFileURL %>" enctype="multipart/form-data" method="post">

<select name="folder">
  <option value="store">Store</option>
  <option value="floor">Floor</option>
  <option value="department">Department</option>
</select>

<aui:input type="file" name="fileupload" />
<aui:button name="Save" value="Save" type="submit" />

</aui:form>

我希望文件上传到所属文件夹中.

i want the file will upload in the belonging folder.

推荐答案

我在将文件上传到指定文件夹方面有一些相似的任务,因此以下是根据您的要求进行了一些修改的代码:

I had somewhat similar task to upload files to specified folders, so following is bit modified code as per your requirement:

public void upload(ActionRequest request, ActionResponse response)
    throws Exception {

    UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
    long sizeInBytes = uploadRequest.getSize(fileInputName);

    if (sizeInBytes == 0) {
        throw new Exception("Received file is 0 bytes!");
    }

    File uploadedFile = uploadRequest.getFile(fileInputName);
    String sourceFileName = uploadRequest.getFileName(fileInputName);

    /* selected folder from UI */
    String paramFolder = uploadRequest.getParameter("folder");

    byte[] bytes = FileUtil.getBytes(uploadedFile);

    if (bytes != null && bytes.length > 0) {
        try {
            /* Create folder if doesn't exist */
            File folder = new File(baseDir + File.separator + paramFolder);
            if (!folder.exists()) {
                folder.mkdir();
            }

            /* Write file to specified location */
            File newFile = new File(folder.getAbsolutePath() + File.separator + sourceFileName);            
            FileOutputStream fileOutputStream = new FileOutputStream(newFile);
            fileOutputStream.write(bytes, 0, bytes.length);
            fileOutputStream.close();           
            newFile = null;

        } catch (FileNotFoundException fnf) {
            newFile = null;
            /* log exception */
        } catch (IOException io) {
            newFile = null;
            /* log exception */
        }
    }
}

这篇关于如何在相对目录中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆