Use the UPLOAD component in JSP

xiaoxiao2021-03-06  87

JSP's support for upload files is not as good as PHP, directly forms a function, nor is it like an ASP to be implemented. JSP can be implemented through JavaBean. But we don't have to write an uploaded bean yourself, there is a lot of forming techniques on the Internet, and smartupload is one of them. However, Smartupload is in the memory of the server first, so the upload too large file (more than 100 trillion) may have problems, and it is not a beautiful middle :)

Let's talk about the page, the SmartUPload component requires the way

. Here is an example upload.htm:


File:
File: file:

Let's take a look at the received pages. After we upload the file to the server, then store it directly into the database: upload.jsp <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ Page Import = "java.sql. *"%> <% @ page import = "com.jspsmart.upload. *"%> <% @ page import = "dbstep.idbManager2000. *"%> <% // instantiation uploading beancom. jspsmart.upload.SmartUpload mySmartUpload = new com.jspsmart.upload.SmartUpload (); // initialize mySmartUpload.initialize (pageContext); // set the maximum value of the carrier mySmartUpload.setMaxFileSize (500 * 1024 * 1024); // file upload MySmartupload.upload (); // Loop acquire all uploaded files for (int i = 0; i Let's download, download two cases 1. Download directly from the database 2. Download from the server

Let me talk about the situation directly from the database: just read the input stream from the database, and then turn to the file.

<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ Page Import = "Java.SQL. *"%> <% @ Page Import = "Java.io. *"%> <% @ page import = "DBstep.iDBManager2000 *."%> <% int bytesum = 0; int byteread = 0; // open the database ResultSet result = null; String Sql = null; PreparedStatement prestmt = null; DBstep.iDBManager2000 DbaObj = new DBstep .idbManager2000 (); dbaobj.openConnection (); // acquired data in the database SQL = "SELECT * from t_local_zhongzhuan"; Result = dbaobj.executeQuery (SQL); result.next ();

/ / Read the data in the database in the stream InputStream Instream = Result.getBinaryStream ("Content"); FileOutputStream Fs = New FileOutputStream ("C: /Dffdsafd.doc");

Byte [] buffer = new byte [1444]; intleth; while ((byteread = instream.read (buffer))! = - 1) {Out.println ("

" byteread "); bytesum = Byteread; system.out.println (bytesum);

fs.write (buffer, 0, byterad);}%>

Again the situation downloaded from the server:

<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.io. *"%> <% string filename = "zsc104.swf" .tostring (); f // Read the infutStream Instream = New FileInputStream ("C: /ZSC104.SWF"); // Set the output format response.reset (); response.setContentType ("bin"); response.addheader ("Content-Disposition" , "attachment; filename = /" " filename " / "); // looped data byte [] b = new byte [100]; int Len; while ((len = instream.read (B) ))> 0) Response.getOutputStream (). Write (b, 0, len); instream.close ();%> Ok, here is not too big to upload the downloaded operation of the download.

Thumbnail implementation, the actual change of the image (JPG, GIF, BMP, etc.) into the desired size import java.io. *; Import java.util. *; Import com.sun.Image.codec.jpeg. *; Import java.awt.image. *; import java.awt. *; import java.net. *; import java.applet. *; import java.sql. *; // thumbnail class, // This Java class can JPG image file, aliquot or non-equivalent size conversion. // Specific use method // s_pic (large image path, generate small picture path, big picture file name, generate small picture name, generate small picture width, generate small picture height, wait for comparison (default to true) PUBLIC Class Small_Pic {String InputDir; // Input Diagram Path String Outputdir; // Output Diagram Path String InputFileName; // Input Diagram File Name String OutputFileName; // Output Diagram File Name INT OUTPUTWIDTH = 80; // Default Output Image Wide Int OutputHeight = 80; // Default output image high int RATE = 0; boolean proportion = true; // Whether to equal than zoom tag (default is equal to scale)

Public small_pic () {// Initialization Variable INPUTDIR = ""; Outputdir = "; InputFileName =" "; OutputFileName =" "; OUTPUTWIDTH = 80; OutputHeight = 80; rate = 0;}

Public void setInputdir (string infutdir) {this.inputdir = inputdir;}

Public void setputdir (string outputdir) {this.outputdir = Outputdir;

Public void setInputFilename (string infutfilename) {this.inputfilename = inputfilename;}

Public void setputFileName (string outputfilename) {this.outputFileName = outputfilename;} public void setputwidth (int outputwidth) {this.outputWidth = OutputWidth;}

Public void setoutputheight (int outputheight) {this.outputHeight = OutputHeight;

Public void setw_h (int width, int Height) {this.outputWidth = width; this.outputheight = height;}

public String s_pic () {BufferedImage image; String NewFileName; // create output file object File file = new File (OutputDir OutputFileName); FileOutputStream tempout = null; try {tempout = new FileOutputStream (file);} catch (Exception ex) {System.out.println (ex. tos = null; Toolkit TK = Toolkit.getDefaultToolkit (); applet app = new applet (); MediaTracker MT = New Mediatracker (app); try {img = TK.GetImage (InputDir InputFileName); Mt.Addimage (IMG, 0); Mt.WaitforId (0);} catch (exception e) {E.printStackTrace ();}

IF (img.getwidth (null) == - 1) {system.out.println ("can't Read, Retry!" "
"); return "no";} else {int new_w; int new_h ; if (this.Proportion == true) // determines whether it is equal to zoom. {// is a picture width and height of the output of the equal scale and high Double Rate1 = (Double) img.getwidth (null)) / (Double) ) OutputWidth 0.1; Double Rate2 = ((Double) img.getHeight (NULL)) / (double) OutputHeight 0.1; double rate = rate1> rate2? Rate1: rate2; new_w = (int) ((Double) IMG. getWidth (NULL)) / RATE); new_h = (int) ((Double) img.getHeight (null)) / rate);} else {new_w = outputWidth; // output picture width new_h = OutputHeight; // output Picture Height} BufferedImage Buffimg = New BufferedImage (new_w, new_h, bufferedimage.type_int_rgb);

Graphics g = buffimg.creategraphics ();

G.SetColor (color.white); g.fillRect (0,0, new_w, new_h);

G.drawImage (IMG, 0, 0, New_W, New_H, NULL); g.dispose ();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (tempout); try {encoder.encode (buffImg); tempout.close ();} catch (IOException ex) {System.out.println (ex.toString ());}} return "ok ";} public String s_pic (String InputDir, String OutputDir, String InputFileName, String OutputFileName) {// enter the path this.InputDir = InputDir; // FIG output path this.OutputDir = OutputDir; // file name enter this. INPUTFILENAME = INPUTFILENAME; // Output diagram file name this.outputFileName = OutputFileName; Return S_PIC ();}

public String s_pic (String InputDir, String OutputDir, String InputFileName, String OutputFileName, int width, int height, boolean gp) {// enter the path this.InputDir = InputDir; // FIG output path this.OutputDir = OutputDir; // Enter the diagram file name this.inputFileName = inputFileName; // Output diagram file name this.outputFileName = OutputFileName; // Set picture length width setw_h (width, height); // Is it equal than zoom tag this.Proportion = gp; return; s_pic ();} public static void main (String [] a) {// s_pic (large image path, generating small picture path, big picture file name, generating small picture name, generating small picture width, generating small picture height) Small_pic mypic = new small_pic (); system.Sout.println (MyPIC.S_PIC ("E: // java // j2eedatum // Wang Liang JSP data // 图 例 // Personal ///" E: // Java // J2EEDATUM // Wine Juxian JSP Data // Tissue Example //PersonAl//"1.jpg", "new1.jpg ", 80, 80 ,true);}

转载请注明原文地址:https://www.9cbs.com/read-122481.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.037, SQL: 9