将文件上传到网站并在上传后打开

时间:2018-12-19 02:30:11

标签: java html portlet

我正在使用Portlet编写网站。现在,我需要编写一个允许用户上传文件的函数,之后后端将对该文件进行一些验证并重新生成一个新文件,然后在用户计算机中打开新文件以允许用户验证。

现在我正在测试上载打开的上载文件部分,但似乎不起作用

html部分:

<form action="<%=uplaodURL%>" method="post" enctype="multipart/form-data">
<input type="file" name="uploadedFile">
<input type="Submit" name="Submit">
</form>

后端(java)

public class DocumentUpload extends MVCPortlet{
    public void uploadDocument(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException,PortletException, PortalException, SystemException 
{

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest); 
    String original_filename = uploadPortletRequest.getFileName("uploadedFile");
    String uploadedTemplateId = uploadPortletRequest.getParameter("templateId");
    System.out.println("uploadedTemplateId:"+uploadedTemplateId);

    File uploadedFile = uploadPortletRequest.getFile("uploadedFile", true); 
    System.out.println("uploadedDocument:" + uploadedFile.getName());
    String fileExt  = getFileExtension(uploadedFile);
    System.out.println("fileExt:" + fileExt);

    Desktop dt= Desktop.getDesktop();
    dt.open(new File(uploadedFile.getAbsolutePath()));


    //renew template name
    String active_data_template_path = "/uploaded_file/";
    System.out.println("auto_gen_filename:" + original_filename);
    System.out.println("active_data_template_path:" + active_data_template_path);

    boolean success = false;
    System.out.println("upload uploadedFile:" + uploadedFile.getName());
    try {
        File activeFolder = new File(active_data_template_path);
        if (!activeFolder.exists()) {
            activeFolder.mkdirs();
        }
        activeFolder.setExecutable(true, false);
        activeFolder.setReadable(true, false);
        activeFolder.setWritable(true, false);
        System.out.println("Active Path - success set permission");

        File auto_gen_file = new File(active_data_template_path + original_filename);
        FileUtil.copyFile(uploadedFile, auto_gen_file);
        success = true;
        System.out.println("success uploadedFile:" + uploadedFile.getName());
        //set permission 777
        auto_gen_file.setExecutable(true, false);
        auto_gen_file.setReadable(true, false);
        auto_gen_file.setWritable(true, false);
        System.out.println("success set permission");
        Desktop dt1= Desktop.getDesktop();
        dt1.open(new File(active_data_template_path + original_filename));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("Failed uploadedFile:" + uploadedFile.getAbsolutePath());
    }



}
}

0 个答案:

没有答案