protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Variable initializations. HttpSession session = request.getSession(); FileItem image_file = null; int record_id = 0; int image_id; // Check if a record ID has been entered. if (request.getParameter("recordID") == null || request.getParameter("recordID").equals("")) { // If no ID has been entered, send message to jsp. response_message = "<p><font color=FF0000>No Record ID Detected, Please Enter One.</font></p>"; session.setAttribute("msg", response_message); response.sendRedirect("UploadImage.jsp"); } try { // Parse the HTTP request to get the image stream. DiskFileUpload fu = new DiskFileUpload(); // Will get multiple image files if that happens and can be accessed through FileItems. List<FileItem> FileItems = fu.parseRequest(request); // Connect to the database and create a statement. conn = getConnected(drivername, dbstring, username, password); stmt = conn.createStatement(); // Process the uploaded items, assuming only 1 image file uploaded. Iterator<FileItem> i = FileItems.iterator(); while (i.hasNext()) { FileItem item = (FileItem) i.next(); // Test if item is a form field and matches recordID. if (item.isFormField()) { if (item.getFieldName().equals("recordID")) { // Covert record id from string to integer. record_id = Integer.parseInt(item.getString()); String sql = "select count(*) from radiology_record where record_id = " + record_id; int count = 0; try { rset = stmt.executeQuery(sql); while (rset != null && rset.next()) { count = (rset.getInt(1)); } } catch (SQLException e) { response_message = e.getMessage(); } // Check if recordID is in the database. if (count == 0) { // Invalid recordID, send message to jsp. response_message = "<p><font color=FF0000>Record ID Does Not Exist In Database.</font></p>"; session.setAttribute("msg", response_message); // Close connection. conn.close(); response.sendRedirect("UploadImage.jsp"); } } } else { image_file = item; if (image_file.getName().equals("")) { // No file, send message to jsp. response_message = "<p><font color=FF0000>No File Selected For Record ID.</font></p>"; session.setAttribute("msg", response_message); // Close connection. conn.close(); response.sendRedirect("UploadImage.jsp"); } } } // Get the image stream. InputStream instream = image_file.getInputStream(); BufferedImage full_image = ImageIO.read(instream); BufferedImage thumbnail = shrink(full_image, 10); BufferedImage regular_image = shrink(full_image, 5); // First, to generate a unique img_id using an SQL sequence. rset1 = stmt.executeQuery("SELECT image_id_sequence.nextval from dual"); rset1.next(); image_id = rset1.getInt(1); // Insert an empty blob into the table first. Note that you have to // use the Oracle specific function empty_blob() to create an empty blob. stmt.execute( "INSERT INTO pacs_images VALUES(" + record_id + "," + image_id + ", empty_blob(), empty_blob(), empty_blob())"); // to retrieve the lob_locator // Note that you must use "FOR UPDATE" in the select statement String cmd = "SELECT * FROM pacs_images WHERE image_id = " + image_id + " FOR UPDATE"; rset = stmt.executeQuery(cmd); rset.next(); BLOB myblobFull = ((OracleResultSet) rset).getBLOB(5); BLOB myblobThumb = ((OracleResultSet) rset).getBLOB(3); BLOB myblobRegular = ((OracleResultSet) rset).getBLOB(4); // Write the full size image to the blob object. OutputStream fullOutstream = myblobFull.getBinaryOutputStream(); ImageIO.write(full_image, "jpg", fullOutstream); // Write the thumbnail size image to the blob object. OutputStream thumbOutstream = myblobThumb.getBinaryOutputStream(); ImageIO.write(thumbnail, "jpg", thumbOutstream); // Write the regular size image to the blob object. OutputStream regularOutstream = myblobRegular.getBinaryOutputStream(); ImageIO.write(regular_image, "jpg", regularOutstream); // Commit the changes to database. stmt.executeUpdate("commit"); response_message = "<p><font color=00CC00>Upload Successful.</font></p>"; session.setAttribute("msg", response_message); instream.close(); fullOutstream.close(); thumbOutstream.close(); regularOutstream.close(); // Close connection. conn.close(); response.sendRedirect("UploadImage.jsp"); instream.close(); fullOutstream.close(); thumbOutstream.close(); regularOutstream.close(); // Close connection. conn.close(); } catch (Exception ex) { response_message = ex.getMessage(); } }
public static void main(String[] args) { try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); ; Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@192.168.117.110:1521:WFZF", "qzkj", "qzkj"); conn.setAutoCommit(false); ; BLOB blob = null; PreparedStatement pstmt = conn.prepareStatement("insert into sys_file(uuid,content,name) values(?,empty_blob(),?)"); String uuid = Tool.getStringUUid(); pstmt.setString(1, uuid); pstmt.setString(2, "test.flv"); pstmt.executeUpdate(); pstmt.close(); pstmt = conn.prepareStatement("select content from sys_file where uuid= ? for update"); pstmt.setString(1, uuid); ResultSet rset = pstmt.executeQuery(); ; if (rset.next()) blob = (BLOB) rset.getBlob(1); ; String fileName = "test.flv"; File f = new File( "C://Documents and Settings//Administrator//桌面//FlvPlayer201002//FlvPlayer201002//" + fileName); FileInputStream fin = new FileInputStream(f); ; System.out.println("file size = " + fin.available()); pstmt = conn.prepareStatement("update sys_file set content=? where uuid=?"); ; OutputStream out = blob.getBinaryOutputStream(); ; int count = -1, total = 0; byte[] data = new byte[(int) fin.available()]; fin.read(data); ; out.write(data); ; /* byte[] data = new byte[blob.getBufferSize();]; 另一种实现方法,节省内存 while ((count = fin.read(data);); != -1); { total += count; out.write(data, 0, count);; } */ fin.close(); ; out.close(); ; pstmt.setBlob(1, blob); ; pstmt.setString(2, uuid); pstmt.executeUpdate(); ; pstmt.close(); ; conn.commit(); ; conn.close(); ; } catch (SQLException e) { System.err.println(e.getMessage()); ; e.printStackTrace(); } catch (IOException e) { System.err.println(e.getMessage()); ; } }
public void lookup(int orgid, int year, DbConn myConn) { String outLine = ""; ResultSet rs = null; Statement stmt = null; // get *active* organizations // get organization name and acronym try { Connection conn = myConn.conn; stmt = conn.createStatement(); String sqlcode = null; String wherestring = null; String orderby = " order by name "; if (orgid > 0) { wherestring = " where isactive = 'T' AND id=" + orgid; sqlcode = "SELECT id, name, acronym, isactive, porgid FROM " + SCHEMAPATH + ".ORGANIZATION " + wherestring; } else { wherestring = " where isactive = 'T'"; sqlcode = "SELECT id, name, acronym, isactive, porgid FROM " + SCHEMAPATH + ".ORGANIZATION " + wherestring + orderby; } // System.out.println(sqlcode); rs = stmt.executeQuery(sqlcode); while (rs.next()) { System.out.println( "/************** " + rs.getString("name") + ", " + rs.getInt("id") + " **************/"); SelfAssessment sa = new SelfAssessment(); SelfAssessment[] item = sa.getSAList(myConn, "id", 0, orgid, year); for (int i = 0; i < item.length; i++) { System.out.println("/**************" + "**************/"); sanumber = item[i].getSANumber(item[i].getOrgid(), item[i].getDivid(), myConn); System.out.println("sanumber: " + sanumber); teamleadid = Integer.valueOf(item[i].getTeamleadid()); String teamleadname = null; teamleadname = item[i].getTeamleadname(teamleadid, myConn); System.out.println("teamleadname: " + teamleadname); title = item[i].getTitle(); System.out.println("title: " + title); purpose = item[i].getPurpose(); System.out.println("purpose: " + purpose); scheduleddate = item[i].getScheduleddate(); System.out.println("scheduleddate: " + scheduleddate); rescheduleddate = item[i].getRescheduleddate(); System.out.println("rescheduleddate: " + rescheduleddate); signeddate = item[i].getSigneddate(); System.out.println("signeddate: " + signeddate); cancelleddate = item[i].getCancelleddate(); System.out.println("cancelleddate: " + cancelleddate); cancelledrationale = item[i].getCancelledrationale(); System.out.println("cancelledrationale: " + cancelledrationale); comments = item[i].getComments(); System.out.println("comments: " + comments); hascirs = item[i].getHascirs(); System.out.println("hascirs: " + hascirs); asstype = item[i].getAsstype(); System.out.println("asstype: " + asstype); assobj = item[i].getAssobj(); System.out.println("assobj: " + assobj); crlevels = item[i].getCrlevels(); System.out.println("crlevels: " + crlevels); crnums = item[i].getCrnums(); System.out.println("crnums: " + crnums); llnums = item[i].getLlnums(); System.out.println("llnums: " + llnums); } } } catch (SQLException e) { outLine = outLine + "SQLException caught: " + e.getMessage(); // log(outLine); // gov.ymp.csi.db.ALog.logError(myConn, 0,"N/A",0, outLine + " - SelfAssessment lookup"); } catch (Exception e) { outLine = outLine + "Exception caught: " + e.getMessage(); // log(outLine); // gov.ymp.csi.db.ALog.logError(myConn, 0,"N/A",0, outLine); } finally { if (rs != null) try { rs.close(); } catch (Exception i) { } if (stmt != null) try { stmt.close(); } catch (Exception i) { } } }