Struts2でのCSVダウンロード方法(その2)
今回は一時ファイル有りver。
@Results( { @Result(name = "download", type = "stream", params = { "inputName", "inputStream", "contentType", "application/octet-stream; charset=UTF-8", "contentLength", "${ contentLength }", "contentDisposition", "attachment; filename = ${fileName}" }) }) @Controller public class SampleAction extends SampleBaseAction implements ModelDriven<SampleModel> { /** デフォルトシリアルバージョンID. */ private static final long serialVersionUID = 1L; /** モデル. */ private SampleModel model = new SampleModel(); /** * ダウンロード. * @return String * @throws SQLException SQL例外 */ public String output() throws SQLException { try { File file = new File(); //ダウンロードさせたいファイル model.setInputStream(new BufferedInputStream(new FileInputStream(file))); model.setFileName(URLEncoder.encode(file.getName(), CSVBeanUtils.DEFAULT_ENC_TYPE)); model.setContentLength(file.length()); } catch (Exception e) { e.printStackTrace(); } return "download"; } @Override public SampleModel getModel() { return model; } }
public class SampleModel implements Serializable {
/** デフォルトシリアルバージョンID. */
private static final long serialVersionUID = 1L;
/** ファイル名. */
private String fileName;
/** ファイル長さ. */
private long contentLength;
/** ストリーム. */
private InputStream inputStream;
/** アップロードファイル. */
private File upload;
/** ファイルコンテキスト. */
private String uploadContextType;
/** ファイル名. */
private String uploadFileName;
//各アクセサ
}