Добавлено: 09 мар 2004, 04:32
Есть такое мнение, что нужно указывать content-length.
Код: Выделить всё
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
performTask(request, response);
//response.setContentType("text/html;charset=Windows-1251");
response.setContentType("image/jpeg");
//response.setContentType("application/download");
//прослеживанию сеанс
HttpSession hs = request.getSession(true);
String s = (String) hs.getValue("correct");
if ((s != null) && (s.equals(ValueCorrectSeanse))) {
try
{
Class.forName(MySQLDriver).newInstance();
Properties connInfo = new Properties();
connInfo.put("user",MySQLUser);
connInfo.put("password",MySQLPassword);
connInfo.put("useUnicode","true");
connInfo.put("characterEncoding",MySQLcharacterEncoding);
//Conn = DriverManager.getConnection(MySQLUrl,MySQLUser,MySQLPassword);
Conn = DriverManager.getConnection(MySQLUrl,connInfo);
Stmt = Conn.createStatement();
//Пытаюсь заюзать базу
Stmt.executeUpdate("USE news");
RS = Stmt.executeQuery("SELECT image FROM News_images where rowid=1");
while (RS.next()) {
com.mysql.jdbc.Blob bl = null;
bl = (com.mysql.jdbc.Blob) RS.getBlob("image");
[b]OutputStream f1 = new FileOutputStream("c:/temp/file111.jpg");
f1.write(bl.getBytes(1,(int) bl.length()));
f1.close();[/b]
response.setContentLength((int) bl.length());
//Создать объект класса ObjectOutputStream.
ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
//запись в поток
out.write(bl.getBytes(1,(int) bl.length()));
//Очистить поток, чтобы быть уверенным в том, что всё содержимое послано клиенту.
out.flush();
out.close();
}
}
catch (Exception E)
{
//pw.println(MesageError);
//pw.println(E);
}
//конец ифа ниже
}
else {
//pw.println("<H2>"+AcessDenied+"<H2>");
}
}
Я плохо разбираюсь в Java. Ты не мог бы привести пример?попробуй воспользоваться Код:
Blob.getBinaryStream()
Код: Выделить всё
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
// !!! BAS !!! performTask(request, response);
response.setContentType("image/jpeg");
//прослеживанию сеанс
HttpSession hs = request.getSession(true);
String s = (String) hs.getValue("correct");
if ((s != null) && (s.equals(ValueCorrectSeanse))) {
try
{
Class.forName(MySQLDriver).newInstance();
Properties connInfo = new Properties();
connInfo.put("user",MySQLUser);
connInfo.put("password",MySQLPassword);
connInfo.put("useUnicode","true");
connInfo.put("characterEncoding",MySQLcharacterEncoding);
//Conn = DriverManager.getConnection(MySQLUrl,MySQLUser,MySQLPassword);
Conn = DriverManager.getConnection(MySQLUrl,connInfo);
Stmt = Conn.createStatement();
//Пытаюсь заюзать базу
Stmt.executeUpdate("USE news");
RS = Stmt.executeQuery("SELECT image FROM News_images where rowid=1");
while (RS.next()) {
com.mysql.jdbc.Blob bl = null;
bl = (com.mysql.jdbc.Blob) RS.getBlob("image");
//Создать объект класса ObjectOutputStream.
OutputStream out = response.getOutputStream();
//запись в поток
out.write(bl.getBytes(1,(int) bl.length()));
//Очистить поток, чтобы быть уверенным в том, что всё содержимое послано клиенту.
out.flush();
out.close();
// !!! BAS !!! to make sure that there are no more images to write
break;
}
}
catch (Exception E)
{
//pw.println(MesageError);
//pw.println(E);
}
//конец ифа ниже
}
else {
//pw.println("<H2>"+AcessDenied+"<H2>");
}
}
Код: Выделить всё
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
performTask(request, response);
//прослеживанию сеанс
HttpSession hs = request.getSession(true);
String s = (String) hs.getValue("correct");
if ((s != null) && (s.equals(ValueCorrectSeanse))) {
try
{
Class.forName(MySQLDriver).newInstance();
Properties connInfo = new Properties();
connInfo.put("user",MySQLUser);
connInfo.put("password",MySQLPassword);
connInfo.put("useUnicode","true");
connInfo.put("characterEncoding",MySQLcharacterEncoding);
//Conn = DriverManager.getConnection(MySQLUrl,MySQLUser,MySQLPassword);
Conn = DriverManager.getConnection(MySQLUrl,connInfo);
Stmt = Conn.createStatement();
//Пытаюсь заюзать базу
Stmt.executeUpdate("USE news");
RS = Stmt.executeQuery("SELECT image FROM News_images where rowid=1");
while (RS.next()) {
com.mysql.jdbc.Blob bl = null;
bl = (com.mysql.jdbc.Blob) RS.getBlob("image");
String someFileNameOnServer = "/images/imageFromDB.jpg";
OutputStream f1 = new FileOutputStream(someFileNameOnServer);
f1.write(bl.getBytes(1,(int) bl.length()));
f1.close();
//запись в поток
PrintWriter writer = new PrintWriter(response.getWriter());
writer.println("<IMG src=\""+someFileNameOnServer+"\" />");
writer.flush();
// !!! BAS !!! to make sure that there are no more images to write
break;
}
}
catch (Exception E)
{
//pw.println(MesageError);
//pw.println(E);
}
//конец ифа ниже
}
else {
//pw.println("<H2>"+AcessDenied+"<H2>");
}
}