Struts in the file upload [posted] Author:
Cold Moon Princess (original)
http://www.xindonghong.com/blog/more.asp?name=wsq&id=666
Cold moon
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.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) && ("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 files Try { InputStream stream = file.getInputStream (); // read a file into String filePath = request.getRealPath ( "/"); // get the current system path ByteArrayOutputStream baos = new ByteArrayOutputStream (); OutputStream bos = new FileOutputStream (filePath " / " file.getFileName ()); // Establish an output stream of an 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 the file to the server} bos.close (); stream.close ();} catch (exception e) {system.rr.rr.print (e);} //request.setttribute ("DAT ", File.GetFileName ()); Return mapping.findforward ("display");} upgran null;}} UPLOADMM.JAVA
Import javax.servlet.http.httpservletRequest; 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;} 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) && (maxL! engthExceeded.booleanValue ())) {errors = new ActionErrors (); errors.add (ERROR_PROPERTY_MAX_LENGTH_EXCEEDED, new ActionError ( "maxLengthExceeded"));} return errors;}} // This is the corresponding form, as well as other properties can be Set, you can refer to the upload example of Struts.
Struts-config.xml
XML Version = "1.0" Encoding = "UTF-8"?>
Reply: Struts file upload
Self-debugging success: Submit page: <% @ page contenttype = "text / html; charset = GB2312" Language = "java" import = "java.sql. *" Errorpage = ""%>