I. Create actionform
Package com. nnhu. Struts. form;
Import javax. servlet. http. httpservletrequest;
Import org. Apache. Struts. Action. actionerror;
Import org. Apache. Struts. Action. actionerrors;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionmapping;
Import org. Apache. Struts. Upload. formfile;
Import org. Apache. Struts. Upload. multipartrequesthandler;
/**
* <P>
* Title: uploadform
* </P>
* <P>
* Copyright: Copyright (c) 2005 techyang http://blog.csdn.net/techyang
* </P>
* @ Author techyang
* @ 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;
Protected formfile thefile2;
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;
}
/**
* @ Return returns the thefile2.
*/
Public formfile getthefile2 ()
{
Return thefile2;
}
/**
* @ Param thefile2 the thefile2 to set.
*/
Public void setthefile2 (formfile thefile2)
{
This. thefile2 = thefile2;
}
}
II. Create actionservlet
Package com. nnhu. Struts. Action;
Import java. Io .*;
Import javax. servlet. http .*;
Import org. Apache. Struts. Action .*;
Import org. Apache. Struts. Upload. formfile;
Import com. nnhu. Struts. Form. uploadform;
/**
* <P>
* Title: uploadaction
* </P>
* <P>
* Copyright: Copyright (c) 2005 techyang http://blog.csdn.net/techyang
* </P>
* @ Author techyang
* @ Version 1.0
*/
Public class uploadaction extends action
{
Public actionforward execute (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response)
Throws exception
{
String encoding = request. getcharacterencoding ();
If (encoding! = NULL) & (encoding. equalsignorecase ("UTF-8 ")))
{
Response. setcontenttype ("text/html; charset = gb2312"); // if no encoding is specified, the encoding format is gb2312.
}
Uploadform theform = (uploadform) form;
Formfile file = theform. getthefile (); // get the uploaded file
Formfile file2 = theform. getthefile2 ();
Try
{
/*
* Take the current system path D:/tomcat5/webapps/coka/where coka is the current context
*/
String filepath = This. getservlet (). getservletcontext ()
. Getrealpath ("/");
Inputstream stream = file. getinputstream (); // read the file
Bytearrayoutputstream baos = new bytearrayoutputstream ();
/*
* Create an output stream for uploading files. If it is a Linux system, replace "//" after uploadfiles "/"
*/
Outputstream Bos = new fileoutputstream (filepath +
"Uploadfiles //" + file. getfilename ());
// D:/tomcat5/webapps/coka/uploadfiles/dsc01508.jpg
/* System. Out. println (filepath +
"Uploadfiles //" + file. getfilename ());
System. Out. println (filepath );*/
Request. setattribute ("FILENAME", 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 ();
Inputstream stream2 = file2.getinputstream (); // read the file
Bytearrayoutputstream baos2 = new bytearrayoutputstream ();
Outputstream bos2 = new fileoutputstream (filepath +
"Uploadfiles //" + file2.getfilename (); // creates an output stream for the uploaded file
Int bytesread2 = 0;
Byte [] buffer2 = new byte [1, 8192];
Int I = 0;
While (bytesread2 = stream2.read (buffer2, 0, 8192 ))! =-1)
{
Bos2.write (buffer2, 0, bytesread2); // write the file to the server
}
Bos2.close ();
Stream2.close ();
} Catch (exception E)
{
System. Err. Print (E );
}
Return Mapping. findforward ("display ");
}
}
3. Create the upload JSP file upload. jsp
<% @ Taglib uri = "http://jakarta.apache.org/struts/tags-html" prefix = "html" %>
<HTML: HTML>
<Head>
<Title> Use struts to upload files </title>
</Head>
<Body>
<HTML: Form Action = "/uploadsaction" enctype = "multipart/form-Data">
<HTML: file property = "thefile"/>
<HTML: file property = "thefile2"/>
<HTML: Submit/>
</Html: Form>
</Body>
</Html: HTML>
4. Configure struts-config.xml files
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype Struts-config public "-// Apache Software Foundation // DTD struts configuration 1.1 //" http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd ">
<Struts-config>
<Data-sources/>
<Form-beans>
<Form-bean name = "uploadsform" type = "com. CNHU. Struts. Form. uploadform"/>
</Form-beans>
<Global-exceptions/>
<Global-forwards>
</Global-forwards>
<Action-mappings>
<Action name = "uploadsform" type = "com. CNHU. Struts. Action. uploadaction" Path = "/uploadsaction">
<Forward name = "display" Path = "/display. jsp"/>
</Action>
</Action-mappings>
</Struts-config>
Full text!