Struts' file upload

xiaoxiao2021-03-06  71

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 the upload file page: selfile.jsp

<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>

UPLOADATION.JAVA

Import java.io. *;

Import javax.servlet.http. *;

Import org.apache.struts.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) && ("UTF-8"))) {response.setContentType ("text / html; charSet = GB2312"); // If No code, encoded format is GB2312} UPLOADFORM Theform = (UPLOADFORM) Form; FormFile File = theform.getthefile (); // Try {INPUTSTREAM Stream = file.getinputStream Stream = file.getinputStream (); // Read files in String FilePath = Request.getRealPath ("/"); // Take the current system path by teArrayOutputStream baos = new ByteArrayOutputStream (); // create a file upload output stream //System.out.println(filePath "/" file; OutputStream bos = new FileOutputStream (filePath "/" file.getFileName ()) .getfilename ()); int Bytesread = 0; byte [] buffer = new byte [8192]; while ((BytesRead = stream.read (buffer, 0, 8192))! = -1) {bos.write (buffer, 0, BYTESREAD; // Write the file to the server} bos.close (); stream.close ();

} Catch (Exception E) {system.err.print (e);} //request.setttribute ("dat", file.getfilename ()); return mapping.findforward ("Display");} return null;}} Uploadform.java

Import javax.servlet.http.httpservletRequest; 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, and other properties can be set, specifically refer to the upload example of Struts.

Struts-config.xml

http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd ">

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

New Post(0)