マッピングされたドライブやサーバーのファイルシステムからファイルを表示、ダウンロードする

=== FACES-CONFIG.XML にコピーペースト===
 
<managed-bean>
    <managed-bean-name>FileShare</managed-bean-name>
    <managed-bean-class>com.acme.FileShare</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>
 
===  XPage かカスタムコントロールにコピーペースト ===
 
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
  <xp:this.beforeRenderResponse><![CDATA[#{javascript:var fp = requestScope.get("filePath");
if(fp!=null){
  FileShare.download(fp)
  requestScope.remove("filePath")
}}]]></xp:this.beforeRenderResponse>
   
  <xp:repeat id="repeat1" rows="30" var="file">
    <xp:this.value><![CDATA[#{javascript:var f = new java.io.File("c:\\temp");
return FileShare().listFilesForFolder(f);}]]></xp:this.value>
    <xp:panel tagName="div">
 
      <xp:link escape="true" text="#{javascript:file}"
        id="link1">
        <xp:eventHandler event="onclick" submit="true"
          refreshMode="complete">
          <xp:this.action><![CDATA[#{javascript:requestScope.put("filePath",file)
var fp = requestScope.get("filePath");
if(fp!=null){
  FileShare.download(fp)
}
}]]></xp:this.action>
        </xp:eventHandler>
      </xp:link>
    </xp:panel>
 
  </xp:repeat></xp:view>
 
 
 
== 新規の JAVA クラス "FileShare" にコピーペースト ==
 
package com.acme;
import java.io.*;
import java.util.*;
import java.net.URLConnection;
import javax.faces.context.FacesContext;
import com.ibm.xsp.webapp.XspHttpServletResponse;
 
 
public class FileShare{
   
  public FileShare(){} 
   
  public static ArrayList listFilesForFolder(final File folder){
    ArrayList<String> files = new ArrayList<String>();
      for(final File fileEntry : folder.listFiles()){
         if (fileEntry.isDirectory()){
              listFilesForFolder(fileEntry);
         }else{
           files.add(fileEntry.getPath());
         }
     }
      return files;
  }
   
  public static byte[] grabFile(String readFile) throws IOException {
 
        File file = new File(readFile);
        ByteArrayOutputStream ous = new ByteArrayOutputStream();
        InputStream ios = new FileInputStream(file);
 
        try {
            byte []buffer = new byte[4096];
 
            int read = 0;
            while ( (read = ios.read(buffer)) != -1 ) {
                ous.write(buffer, 0, read);
            }
        } finally { 
            try {
                 if ( ous != null ) 
                     ous.close();
            } catch ( IOException e) {
            }
 
            try {
                 if ( ios != null ) 
                      ios.close();
            } catch ( IOException e) {
            }
        }
        return ous.toByteArray();
    }
 
  public static void download(String filePath) throws IOException {
 
    String fn = new File(filePath).getName();
    byte[] data = grabFile(filePath);
        XspHttpServletResponse response = (XspHttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.setContentType(URLConnection.guessContentTypeFromName(fn));
        response.setHeader("Content-disposition", "attachment; filename=" + fn);
        OutputStream output = response.getOutputStream();
        output.write(data);
        output.close();
        FacesContext.getCurrentInstance().responseComplete(); 
    }
   
}





このコードは繰り返しコントロール内でサーバーからサブフォルダーを含むどのディレクトリー内のファイルを表示します。ブラウザーからはクリックしてファイルをダウンロードすることも可能です。例えば、サーバー上のマッピングされたフォルダーを指定して、ダウンロード用にフォルダー内のすべてのファイルを表示します。例 P:¥ACME¥

- 動作には java ポリシーを更新する必要があるかもしれません
- Windows 環境のみテスト

Java
katoman
August 19, 2015 at 11:07 AM
Rating
0





No comments yetLogin first to comment...