Struts' file upload

xiaoxiao2021-03-06  41

I have just finished a project recently, do it with struts1.1. Never understand, to skilled, rely on some documents of 9CBS. But there is not necessarily suitable for yourself, so I will take some things I have to give you some things, and I will communicate with each other. If you have a good way, you can contact me. MSN: WHW_DREAM (AT) Hotmail.comStruts Upload this text is Struts1.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 "%> ------------------------------- ------------------------------------ UPLOADACTION .java ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------- Import java.io. *; import javax.servlet.http. *; import Org .apache.struts.Action. *; import org.apache.struts.upload.formfile; / ** *

Title: UploadAction *

Description: Qrrsmms *

CopyRight : Copyright (c) 2004 jiahansoft *

Company: jiahansoft * @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 *

Description: Qrrsmms *

Copyright: Copyright (c) 2004 jiahansoft *

Company: jiahansoft * @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;

} Public ActionErrors validate (ActionMapping mapping, HttpServletRequest request) {ActionErrors errors = null; // has the maximum length been exceeded Boolean maxLengthExceeded = (Boolean) request.getAttribute (MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);?! If ((maxLengthExceeded = null) && (maxLengthExceeded.booleanvalue ())) {errors = new ActionErrors (); errors.add (ERROR_PROPERTY_MAX_LENGTH_EXCEEDED, new ActionError ( "maxLengthExceeded"));} return errors;}} // this is the corresponding form, there are other properties It can be provided that specifically refer to the upload example of Struts. -------------------------------------------------- ----------------------------------------- -------------------------------------------------- ---------------

;>

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

New Post(0)