1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| package view.demo;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import oracle.jbo.ApplicationModule;
import oracle.jbo.Key;
import oracle.jbo.Row;
import oracle.jbo.ViewObject;
import oracle.jbo.client.Configuration;
import oracle.jbo.domain.BlobDomain;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Servlet1 extends HttpServlet {
private static final Log LOG = LogFactory.getLog(Servlet1.class);
private static String lastPic = "";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id=request.getParameter("id");
if (id != null) {
lastPic = id;
}
String imgno=request.getParameter("imgno");
String appModuleName = "demo.model.AppModule";
String appModuleConfig = "AppModuleLocal";
String voQuery = "";
if (id == null) {
voQuery = "select employee_pic from pics where employee_id = '"+lastPic+"'";}
else{
voQuery = "select employee_pic from pics where employee_id = '"+id+"'";
}
String mimeType = "jpeg";
ApplicationModule am = Configuration.createRootApplicationModule(appModuleName, appModuleConfig);
ViewObject vo = am.createViewObjectFromQueryStmt("picsView", voQuery);
// Run the query
vo.executeQuery();
// Get the result (only the first row is taken into account)
Row product = vo.first();
BlobDomain image = null;
// Check if a row has been found
if (product != null) {
// We assume the Blob to be the first a field
image = (BlobDomain)product.getAttribute(0);
if (product.getAttributeCount() > 1) {
mimeType = (String)product.getAttribute(1);
}
} else {
LOG.warn("No row found to get image from !!!");
return;
}
response.setContentType("image/" + mimeType + "; charset=windows-1252");
OutputStream os = response.getOutputStream();
InputStream is = image.getInputStream();
// copy blob to output
byte[] buffer = new byte[4096];
int nread;
while ((nread = is.read(buffer)) != -1) {
os.write(buffer, 0, nread);
}
os.close();
// Remove the temporary viewobject
vo.remove();
// Release the appModule
Configuration.releaseRootApplicationModule(am, false);
}
} |
Partager