反射
通過反射可以設(shè)置類的私有屬性
Field f1 = Student.class.getDeclaredField("name");
f1.setAccessible(true);
f1.set(stu1, "Li zxc");
Field f2 = Student.class.getDeclaredField("age");
f2.setAccessible(true);
f2.set(stu1, 20);
通過反射可以使用類的方法
String methodName = "eat";
Method m = Student.class.getDeclaredMethod(methodName);
m.invoke(stu1);
文件上傳
MySQL
文件類型為 longblob
jsp
如果要向服務(wù)器傳文件竿奏,需要在form屬性添加enctype="multipart/form-data"
<form action="add_emp.do" method="post" enctype="multipart/form-data">
input類型是file
比如說:照片
照片: <input type="file" name="photo">
Servlet
支持文件上傳
在以前市咆,處理文件上傳是一個(gè)很痛苦的事情,大都借助于開源的上傳組件登钥,諸如commons fileupload等。
現(xiàn)在Servlet 3.0文件上傳支持闷沥。以前的HTML端上傳表單不用改變什么母赵,還是一樣的multipart/form-data MIME類型眼溶。
讓Servlet支持上傳,需要做兩件事情
1.需要添加MultipartConfig注解
@MultipartConfig
2.從request對象中獲取Part文件對象幅疼,并通過part的輸入流將文件寫入到buffer緩沖區(qū)
Part part = req.getPart("photo");
byte[] buffer = new byte[(int) part.getSize()];
part.getInputStream().read(buffer);
字符串日期轉(zhuǎn)換為Date類型
一般使用SimpleDateFormat 創(chuàng)建一個(gè)格式器來格式化字符串日期為Date類型米奸,最好把這種方法封裝到工具類里面
private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
public static Date stringToDate(String pattern, String dateStr) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
try {
return formatter.parse(dateStr);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
public static Date stringToDate(String dateStr) {
return stringToDate(DEFAULT_DATE_PATTERN, dateStr);
}
解決重復(fù)代碼
當(dāng)A類繼承B類,有許多類似A類中都要實(shí)現(xiàn)相同的方法爽篷,如果要解決這些相同的重復(fù)代碼悴晰,可以考慮建一個(gè)C類繼承B類,把重復(fù)方法寫在C類逐工,屬性設(shè)為protected铡溪,讓A類繼承C類,這樣就不用每個(gè)類似A類的類都要寫這種重復(fù)的方法泪喊。
例子:
public class BaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected DeptService getDeptService() {
return (DeptService) ServiceFactory.factory(DeptService.class);
}
protected EmpService getEmpService() {
return (EmpService) ServiceFactory.factory(EmpService.class);
}
}
jsp顯示圖片
數(shù)據(jù)庫圖片存放的是longblob棕硫,byte數(shù)組
例子:根據(jù)員工編號拿到photo
1.第一步 html
<i*m*g src="show_photo.do?eno=${emp.id}" width="150px" height="200px">
如果支持多文件上傳input添加屬性multiple
- 第二步 實(shí)現(xiàn)dao層byte[] findPhotoById(int id)方法
public byte[] findPhotoById(int id) {
try (ResultSet rs = DbSessionFactory.openSession().executeQuery(
"select photo from tb_emp where eno=?", id)) {
if (rs.next()) {
return rs.getBytes("photo");
}
return null;
}
catch (SQLException e) {
e.printStackTrace();
throw new DbException("處理結(jié)果集異常", e);
}
}
3.第三步 實(shí)現(xiàn)biz業(yè)務(wù)層BufferedImage getEmpPhoto(int empId)方法,通過dao層findPhotoById()方法得到字節(jié)數(shù)組袒啼,然后通過ByteArrayInputStream流讀取buffer到輸入流哈扮,最后通過ImageIO類的read()方法傳入輸入流返回BufferedImage
public BufferedImage getEmpPhoto(int empId) {
byte[] buffer = empdao.findPhotoById(empId);
if (buffer != null && buffer.length > 0) {
try (InputStream in = new ByteArrayInputStream(buffer)) {
return ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
4.第四步 創(chuàng)建servlet,根據(jù)業(yè)務(wù)層方法得到緩沖圖片瘤泪,如果不為空灶泵,用ImageIO.write()輸出
@WebServlet(urlPatterns="/show_photo.do", loadOnStartup=1)
public class GetEmpPhotoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest rep, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("image/jpeg");
String enoStr = rep.getParameter("eno");
if (enoStr != null) {
int empId = Integer.parseInt(enoStr);
BufferedImage photo = getEmpService().getEmpPhoto(empId);
if (photo != null) {
ImageIO.write(photo, "JPG", resp.getOutputStream());
}
}
}
private EmpService getEmpService() {
return (EmpService) ServiceFactory.factory(EmpService.class);
}
}
注意:
需要先設(shè)置輸出流內(nèi)容格式為圖片格式 resp.setContentType("image/jpeg");