Created
March 19, 2014 07:37
-
-
Save masazdream/9637089 to your computer and use it in GitHub Desktop.
Servletで画像を受け取るサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| try { | |
| request.setCharacterEncoding("UTF-8"); | |
| String type = request.getParameter("type"); | |
| System.out.println(type); | |
| if (type == null) { | |
| response.getWriter().println("type parameter failed."); | |
| } else { | |
| OutputStream out = null; | |
| InputStream in = null; | |
| try { | |
| response.setContentType("application/octet-stream"); | |
| response.setHeader("Content-Disposition", | |
| "filename=\"ファイル名\""); | |
| in = new FileInputStream("物理ファイル名"); | |
| out = response.getOutputStream(); | |
| byte[] buff = new byte[1024]; | |
| int len = 0; | |
| while ((len = in.read(buff, 0, buff.length)) != -1) { | |
| out.write(buff, 0, len); | |
| } | |
| } finally { | |
| if (in != null) { | |
| try { | |
| in.close(); | |
| } catch (IOException e) { | |
| } | |
| } | |
| if (out != null) { | |
| try { | |
| out.close(); | |
| } catch (IOException e) { | |
| } | |
| } | |
| } | |
| } | |
| } catch (IOException e) { | |
| // Error | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment