jsp读取并显示图片的问题
1. ssh框架 工程的发布目录已经改成tomcat下目录 在该目录下建了一个放图片的文件夹 tomcat重启后此文件夹消失了怎么办?2. 我用ajax读取数据库中存的图片的地址,然后更改src的值,不能显示.数据库里到底应该存什么路径,是文件的的绝对路径还是相对路径?
3. 有没有别的存图片并且显示的方法?
action代码
String tea_number = (String) request.getSession().getAttribute("tea_number");
String type = getUploadFileName().substring(getUploadFileName().lastIndexOf(".")).toLowerCase();
String realpath = request.getSession().getServletContext().getRealPath("\\images\\"+tea_number+type);
String path = "/images/"+tea_number+type;
FileInputStream fis = new FileInputStream(getUpload());
FileOutputStream fos = new FileOutputStream(realpath);
int len;
byte[] buf = new byte[1024];
while((len=fis.read(buf))!=-1){
fos.write(buf,0,len);
}
fos.close();
fis.close();
Teacher t = new Teacher();
t.setTea_number(tea_number);
t = teacherService.findSingleTeacher(t);
t.setTea_icon(path);
teacherService.updateTeacher(t);
--------------------编程问答-------------------- 如果你愿意,你可以吧图片存成二进制数据。。
我一般存储是绝对路径,相对于工程文件夹的话我觉得很别扭。
--------------------编程问答--------------------
请问存成二进制文件之后怎么读取呢 --------------------编程问答-------------------- 读取二进制例子
--------------------编程问答-------------------- 绝对路径算了,路径名在数据库或者配置文件中读取
/**
* 获取保安人员相片
* @param id
* @param response
* @throws Exception
*/
@RequestMapping("/getpicuture")
public void getPicture(@RequestParam("tableName") String tableName,
@RequestParam("keyFieldName") String keyFieldName,
@RequestParam("keyFieldValue") String keyFieldValue,
@RequestParam("imageFieldName") String imageFieldName,
@RequestParam("time") long time, HttpServletResponse response,
HttpServletRequest request) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
byte[] by = null;
try {
conn = beanFactoryDataSourceLookup.getDataSource("dataSource")
.getConnection();
String sql = "select " + imageFieldName + " from " + tableName
+ " t where t." + keyFieldName + " = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, keyFieldValue);
rs = ps.executeQuery();
if (rs.next()) {
Blob bo = rs.getBlob(1);
by = new byte[(int) bo.length()];
InputStream in = bo.getBinaryStream();
in.read(by);
ServletOutputStream out = response.getOutputStream();
in.close();
out.write(by);
out.flush();
out.close();
in.close();
}
} catch (Exception e) {
// e.printStackTrace();
} finally {
conn.close();
}
}
补充:Java , Web 开发