td> form> 2. Read the table SSSGetData.jsp
======================================================== - > <% @ Page ContentType = "text / html; charset = GBK"%> <% @ page language = "java" import = "com.jspsmart.upload. *"%> <% @ page import = "java.text . SimpleDateFormat "%> <% @ page import =" java.io.file "%> <% @ page import =" java.util. * "%> <% @ Page import =" javazoom.upload. * "%> <% @ Page Import = "Uploadutilities.Filemover"%>
head> <% Request.setCharacterencoding (" GBK "); // Setting the encoding format, no need to transfer one by one. FileMover filemover = new filemover (); // You can also use the JSP: UseBean in an instance. UploadBean upBean = new UploadBean (); MultipartFormDataRequest mrequest = null; Hashtable files = null; if (MultipartFormDataRequest.isMultipartFormData (request)) {mrequest = new MultipartFormDataRequest (request, null, 100 * 1024 * 1024, MultipartFormDataRequest.COSPARSER, "GBK" ); // Note that the encoding parameter String StT0 = MREQUEST.GETPARAMETER ("title"); " TITLE0 is:" stt0 " "); string stt1 = new String ( Stt0.GetBytes ("ISO-8859-1"), "GBK"); Out.println (" Title1 is:" stt1 " "); // This is used to test whether the Title parameter is correct. When debugging, add a sentence if (true) return;
files = mrequest.getFiles ();} // get file name before modification String sOldFileName = mrequest.getParameter ( "oldfilename"); out.println ( "sOldFileName:" sOldFileName); String sWebRootPath = request.getRealPath ( "/ "); // get the root of your web application. String spath = SWEBROOTPATH "attach"; int ifilecount = 0; string sserverfilename = ""; string slocalfilename = ""; // file gets IF ((files! = Null) | (! Files.isempty ())) {iFileCount = files.size (); UploadFile file = (UploadFile) files.get ( "attach"); sLocalFileName = file.getFileName (); out.println ( "sLocalFileName:" sLocalFileName); int ii = sLocalFileName.indexOf ( " "); // Take the suffix string sext = slocalfilename.substring (ii, slocalfilename.length ()); // Get no repetition name java.util.date dt = new java.util.date (System) .currentTimeMillis ()); SimpleDateFormat fmt = new SimpleDateFormat ( "yyyyMMddHHmmssSSS"); sServerFileName = fmt.format (dt); sServerFileName = sServerFileName sExt; // if the directory does not exist, a new File dir = new File (sPath ); If (! Dir.exists ()) {dir.mkdirs ();} UpBean.SetFolderstore (spath); // Set the directory UPBean.adduploadListener (filemover) you want to upload; // Add Filmover Monitor FileMover.setNewFileName (SserveFileName ); // Set file name upbean.store on the server (MREQUEST, "Attach"); // Upload Out.Println ("File Path IS" "/" SserverFileName);}%> Demo Notes: cos.jar, UploadBean.jar, filemover.jar These packs must be available.
Must Download Address: UploadBean 1.5: http://wcarch/winnt/java/uploadbean1_5.zipfilemover 1.3: http://www.javazoom.net/jzservlets/uploadbean/addons/filemover1 .3.zip Reference Download Address: Chinesupload example: http://www.javazoom.net/jzservlets/uploadbean/addons/chineseupload.zip Refer to FBYSSS 2. ORG.APache.Struts.upload.formfileStruts 1.1.1 Org.apache.struts.upload.formfile class. Very convenient, don't write it yourself. You don't have to write a JSP to call JSPSmartupLoad to get it.
Select upload file page: selfile.jsp ------------------------------------------ -------------------------------------- <% @ Taglib URI = "/ WEB-INF / Struts-html.tld "prefix =" html "%> html: form> html: html> ------------------------------- ------------------------------------ UPLOADACTION .java ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------- Import java.io. *; import javax.servlet.http. *; import Org .apache.struts.Action. *; import org.apache.struts.upload.formfile; / *** Title: UploadAction p> *
description: Qrrsmms p> *
CopyRight : Copyright (c) 2004 jiahansoft p> *
Company: jiahansoft p> * @author wanghw * @version 1.0 * / public class UpLoadAction extends Action {public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request , HttpServletResponse response) throws Exception {if (form instanceof uploadsForm) {// If the form is uploadsForm String encoding = request.getCharacterEncoding ();! If ((encoding = null) && (encoding.equalsIgnoreCase ( "utf-8"))) {Response.setContentType ("text / html; charset = GB2312"); // If no code is specified, the encoded format is GB2312} UPLOADFORM Theform = (UPLOADFORM) Form; FormFile File = theform.getthefile (); // Get uploaded Document Try {INPUTSTREAM Stream = file.getinputStream ();
// read a file into String filePath = request.getRealPath ( "/"); // get the system path ByteArrayOutputStream baos = new ByteArrayOutputStream current (); OutputStream bos = new FileOutputStream (filePath "/" file.getFileName ()) ; // Create an output stream of upload file //system.out.println (FilePath "/" file.getFileName ()); int BytesRead = 0; byte [] buffer = new byte [8192]; while ((bytesRead) = stream.read (buffer, 0, 8192))! = -1) {bos.write (buffer, 0, bytesread); // Write files to serve the server} Bos.Close (); stream.close ();} Catch (Exception E) {system.err.print (e);} //request.settribute ("dat", file.getfilename ()); return mapping.findforward ("display");} return null;}} -------------------------------------------------- ---------------------------- UPLOADFORM.JAVA -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ------------ Import javax.servlet.http.http ServletRequest; import org.apache.struts.action. *; Import org.apache.struts.upload. *; / ***
Title: Uploadform p> *
Description: Qrrsmms p> *
Copyright: Copyright (c) 2004 jiahansoft p> *
Company: jiahansoft p> * @author wanghw * @version 1.0 * / public class upLoadForm extends ActionForm {public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org .apache.struts.webapp.upload.MaxLengthExceeded "; protected FormFile theFile; public FormFile getTheFile () {return theFile;} public void setTheFile (FormFile theFile) {this.theFile = theFile;
转载请注明原文地址:https://www.9cbs.com/read-47082.html