「Spring MVC Tips」の版間の差分
ナビゲーションに移動
検索に移動
| 1行目: | 1行目: | ||
| − | ==Spring MVC Tips== | + | ==[[Spring MVC Tips]]== |
[[Spring]] | [[Spring MVC]] | | [[Spring]] | [[Spring MVC]] | | ||
==コントローラー== | ==コントローラー== | ||
===リクエストパラメータを必須でなくする=== | ===リクエストパラメータを必須でなくする=== | ||
| − | *@ | + | *@[[R]]equestParam を指定するとデフォルトで必須なのでパラメータがない場合、U[[R]]Lにマッチしない |
*required=falseとする | *required=falseとする | ||
| − | @ | + | @[[R]]equestMapping(value="/test.html",method = [[R]]equestMethod.GET) |
| − | public String setupForm( @ | + | public String setupForm( @[[R]]equestParam(value="id", required=false) String id, ModelMap model) { |
: | : | ||
| 13行目: | 13行目: | ||
===ファイルアップロード=== | ===ファイルアップロード=== | ||
*MultipartFile を利用する | *MultipartFile を利用する | ||
| − | * | + | *[[文字化け]]対策をする |
=====Form===== | =====Form===== | ||
<form id="formUpload" action="upload.html" method="POST" enctype="multipart/form-data" > | <form id="formUpload" action="upload.html" method="POST" enctype="multipart/form-data" > | ||
| 20行目: | 20行目: | ||
</form> | </form> | ||
| − | ===== | + | =====[[文字コード]]をiso-8859-1として変換===== |
| − | *http://ja.wikipedia.org/wiki/ISO/ | + | *http://ja.wikipedia.org/wiki/ISO/[[IE]]C_8859-1 |
| − | @ | + | @[[R]]equestMapping(value="/upload.html", method = [[R]]equestMethod.POST) |
public String uploadFile(MultipartFile file) { | public String uploadFile(MultipartFile file) { | ||
String filename = new String(file.getOriginalFilename().getBytes("iso-8859-1"),"utf-8"); | String filename = new String(file.getOriginalFilename().getBytes("iso-8859-1"),"utf-8"); | ||
===ファイルダウンロード=== | ===ファイルダウンロード=== | ||
| − | //String fileName = new String(originalFileName.getBytes("utf-8"),"iso-8859-1"); // Chrome OK, IE NG | + | //String fileName = new String(originalFileName.getBytes("utf-8"),"iso-8859-1"); // Chrome OK, [[IE]] NG |
| − | String fileName = new String(originalFileName.getBytes("ms932"),"iso-8859-1"); // Chrome OK, IE OK | + | String fileName = new String(originalFileName.getBytes("ms932"),"iso-8859-1"); // Chrome OK, [[IE]] OK |
| − | response. | + | response.set[[Header]]("Content-Type", "application/octet-stream"); |
| − | response. | + | response.set[[Header]]("Content-Disposition", "filename=\"" + fileName + "\""); |
| − | BufferedInputStream bis = new BufferedInputStream( | + | BufferedInputStream bis = new BufferedInputStream(attachFileDownloadSer[[vi]]ce.getAttachFileOutputStream(attachFileInfo)); |
os = response.getOutputStream(); | os = response.getOutputStream(); | ||
2020年2月16日 (日) 04:32時点における最新版
目次
Spring MVC Tips
Spring | Spring MVC |
コントローラー
リクエストパラメータを必須でなくする
@RequestMapping(value="/test.html",method = RequestMethod.GET) public String setupForm( @RequestParam(value="id", required=false) String id, ModelMap model) { :
ファイルアップロード
- MultipartFile を利用する
- 文字化け対策をする
Form
<form id="formUpload" action="upload.html" method="POST" enctype="multipart/form-data" > <input name="attach_file" /> <input type="submit" value="アップロード"/> </form>
文字コードをiso-8859-1として変換
@RequestMapping(value="/upload.html", method = RequestMethod.POST) public String uploadFile(MultipartFile file) { String filename = new String(file.getOriginalFilename().getBytes("iso-8859-1"),"utf-8");
ファイルダウンロード
//String fileName = new String(originalFileName.getBytes("utf-8"),"iso-8859-1"); // Chrome OK, IE NG
String fileName = new String(originalFileName.getBytes("ms932"),"iso-8859-1"); // Chrome OK, IE OK
response.setHeader("Content-Type", "application/octet-stream");
response.setHeader("Content-Disposition", "filename=\"" + fileName + "\"");
BufferedInputStream bis = new BufferedInputStream(attachFileDownloadService.getAttachFileOutputStream(attachFileInfo));
os = response.getOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = bis.read(buffer)) >= 0) {
os.write(buffer,0, len);
}
bis.close();
© 2006 矢木浩人