Skip to content

Instantly share code, notes, and snippets.

@masazdream
Created March 19, 2014 07:37
Show Gist options
  • Select an option

  • Save masazdream/9637089 to your computer and use it in GitHub Desktop.

Select an option

Save masazdream/9637089 to your computer and use it in GitHub Desktop.
Servletで画像を受け取るサンプル
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