Skip to content

Instantly share code, notes, and snippets.

@yeluolanyan
Last active October 8, 2015 14:38
Show Gist options
  • Save yeluolanyan/3346064 to your computer and use it in GitHub Desktop.
Save yeluolanyan/3346064 to your computer and use it in GitHub Desktop.
头像图片显示
//img.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'img.jsp' starting page</title>
</head>
<body>
<img alt="" src="img.jspx?img=2.png">
</body>
</html>
//imgServlet文件
package com.kaishengit.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ImgServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String img = request.getParameter("img");
File file = new File("C:/upload",img);
if(file.exists()) {
InputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[512];
int len = -1;
while((len = in.read(buffer)) != -1) {
out.write(buffer,0,len);
}
out.flush();
in.close();
out.close();
} else {
response.sendError(404);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment