0

我正在使用 Struts 2 构建一个应用程序。我在其中有这一部分,我让用户可以选择上传他们想要的任何 pdf 文件。这将存储在我在 G 驱动器中创建的文件夹中。现在我想添加一个选项,用户可以在其中查看目录中存在的文件列表并查看或删除它。我已经添加了一个有助于查看列表的 jsp。

package net.admin.module;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{

    private File fileUpload;
    private String fileUploadContentType;
    private String fileUploadFileName;
    private String destPath;

    public String getFileUploadContentType() {
        return fileUploadContentType;
    }

    public void setFileUploadContentType(String fileUploadContentType) {
        this.fileUploadContentType = fileUploadContentType;
    }

    public String getFileUploadFileName() {
        return fileUploadFileName;
    }

    public void setFileUploadFileName(String fileUploadFileName) {
        this.fileUploadFileName = fileUploadFileName;
    }

    public File getFileUpload() {
        return fileUpload;
    }

    public void setFileUpload(File fileUpload) {
        this.fileUpload = fileUpload;
    }

    public String execute() 
    {
    String targetPath = "G:/FileUploadAction/";
    File fileToCreate = new File(targetPath, fileUploadFileName);
            try
            {
                    FileUtils.copyFile(this.fileUpload, fileToCreate);
            }
            catch (IOException e) 
            {
                    addActionError(e.getMessage());
            }
            return SUCCESS;
    }

    public String display() {
        return NONE;
    }

}

这是我的行动课

<%@ page language="java" import="java.sql.*,java.io.*,java.util.*" errorPage="" %> 
<%! 
public static List recurseDir(String dir) { 
      String result, _result[]; 

      result = recurseInDirFrom(dir); 
      _result = result.split("\\|"); 
      return Arrays.asList(_result); 
} 

  private static String recurseInDirFrom(String dirItem) { 
    File file; 
    String list[], result; 

    result = dirItem; 

    file = new File(dirItem); 
    if (file.isDirectory()) { 
      list = file.list(); 
      for (int i = 0; i < list.length; i++) 
        result = result + "|"  + recurseInDirFrom(dirItem + File.separatorChar + list[i]); 
      } 
    return result; 
  } 
%> 
<% 
String path = "G:/FileUploadAction/";  
     List sd=recurseDir(path); 
     for(int i=0;i<sd.size();i++) 
     { 
          out.println(i+"  "+sd.get(i)+"<br>"); 
      } 
%>
<%
File f=new File("G:/FileUploadAction/Holiday List.pdf");

f.delete();

%>

这是我的jsp,它列出了已经存在的文件。

我怎样才能继续查看并删除它?

4

0 回答 0