📄 gsbinarydownload.java
字号:
package jp.co.sjts.gsession.tools;
import java.io.File;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import javax.servlet.http.HttpServletResponse;
/**
* <p>僶僀僫儕僼傽僀儖僟僂儞儘乕僪僋儔僗 GSBinaryDownLoad.java
*
* @author Satoru K <koni@sjts.co.jp>
* @author Junichi K <kaneuchi@sjts.co.jp>
*/
public class GSBinaryDownLoad {
private File file;
private String dstFilename;
private String contentType="application/octet-stream";
/**
* 僐儞僗僩儔僋僞
*
* @param java.lang.String path : 僟僂儞儘乕僪僼傽僀儖偺僷僗
* @param java.lang.String dstFilename : 僟僂儞儘乕僪僼傽僀儖柤
*/
public GSBinaryDownLoad(String path,String dstFilename) {
file = new File(path);
this.dstFilename = dstFilename;
this.contentType = ContentType.getContentType(dstFilename);
}
/**
* 僐儞僗僩儔僋僞
*
* @param java.lang.String path : 僟僂儞儘乕僪僼傽僀儖偺僷僗
* @param java.lang.String dstFilename : 僟僂儞儘乕僪僼傽僀儖柤
* @param java.lang.String contentType : 僟僂儞儘乕僪僼傽僀儖偺content-type
*/
public GSBinaryDownLoad(String path,String dstFilename,String contentType) {
file = new File(path);
this.dstFilename = dstFilename;
this.contentType = contentType;
}
/**
* 僟僂儞儘乕僪僼傽僀儖偺content-type傪巜掕偟傑偡丅
*
* @param java.lang.String contentType : 僟僂儞儘乕僪僼傽僀儖偺content-type
*/
public void setContentType(String contentType) {
this.contentType = contentType;
}
/**
* 僐儞僗僩儔僋僞偱巜掕偟偨僼傽僀儖偺僟僂儞儘乕僪僂傿儞僪僂傪昞帵偟傑偡丅
*
* @param javax.servlet.http.HttpServletResponse res
*/
public void write(HttpServletResponse res) throws GSException {
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out = new BufferedOutputStream(res.getOutputStream());
res.setContentType(contentType);
// res.setHeader("Content-Disposition","attachment; filename=\"" + dstFilename + "\"");
res.setHeader("Content-Disposition","attachment; filename=\"" + URLEncoder.encode(dstFilename,"UTF-8") + "\"");
// res.setHeader("Content-Description","file download");
res.setContentLength((int)file.length());
int i;
while((i=in.read()) >= 0){
out.write(i);
}
out.flush();
in.close();
out.close();
} catch (FileNotFoundException e){
} catch (IOException e){
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -