Struts2でimgタグの画像リクエストを受け取って返す
<result-types> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>←コレ追加 </result-types>
Modelに追加
private String contentType;←多分必須。 private Long contentLength; private String contentDisposition; private String inputName; private String bufferSize; private InputStream inputStream;←多分必須。 Action @Results({ @Result(name = "stream", type = "stream") }) @Controller public class SampleAction extends SampleBaseAction implements ModelDriven<SampleModel> { /** デフォルトシリアルバージョンID. */ private static final long serialVersionUID = 1L; /** モデル. */ private SampleModel model = new SampleModel(); //試し public String image() { try { model.setContentType("image/jpeg"); String filePath = FileConfig.getBaseImageDir() + "anythingcuter_thanthis.jpg"; File file = new File(filePath); model.setInputStream(FileUtils.openInputStream(file)); // ダウンロード時のファイル名を定義 model.setContentDisposition("filename=\"sample.jpeg\""); } catch (Exception e) { e.printStackTrace(); } return "stream"; } @Override public SampleModel getModel() { return model; } }
HTML
<img alt="aaaaa" src="/sample!image">