Upload
Recently encountered the problem of uploading multiple files while doing a struts project. On the internet to check a lot of information, also did not find the use of struts to upload multiple files examples. After several days of research, I realized the ability to upload multiple files with struts. Now post it for everyone to share!
one. Establish Actionform
Package com.cnehu.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) 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;
}
}
Two. Establish Actionservlet
Package com.cnehu.struts.action;
Import java.io.*;
Import javax.servlet.http.*;
Import org.apache.struts.action.*;
Import Org.apache.struts.upload.FormFile;
Import Com.cnehu.struts.form.UpLoadForm;
/**
* <p>
* Title:uploadaction
* </p>
* <p>
* Copyright:copyright (c) 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 uploaded files
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 into
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
/*
* Create an output stream of uploaded files if it is a Linux system, please replace the uploadfiles "\" with "/"
*/
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 file to server
}
Bos.close ();
Stream.Close ();
InputStream stream2 = File2.getinputstream ()//read the file into
Bytearrayoutputstream Baos2 = new Bytearrayoutputstream ();
OutputStream Bos2 = new FileOutputStream (FilePath +
"Uploadfiles\\" +file2.getfilename ())//create an output stream of uploaded files
int bytesRead2 = 0;
byte[] Buffer2 = new byte[8192];
int i=0;
while ((bytesRead2 = Stream2.read (buffer2, 0, 8192))!=-1)
{
Bos2.write (buffer2, 0, bytesRead2);//write file to server
}
Bos2.close ();
Stream2.close ();
catch (Exception e)
{
System.err.print (e);
}
Return Mapping.findforward ("display");
}
}
Three. Build JSP file for upload upload.jsp
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-html" prefix= "html"%>
<title> upload file with struts </title>
<body>
</body>
Four. Configuring Struts-config.xml Files
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts-config Public "-//apache Software foundation//dtd struts Configuration 1.1//en" "http:// Jakarta.apache.org/struts/dtds/struts-config_1_1.dtd ">
<struts-config>
<data-sources/>
<form-beans >
<form-bean name= "Uploadsform" type= "Com.cnehu.struts.form.UpLoadForm"/>
</form-beans>
<global-exceptions/>
<global-forwards >
</global-forwards>
<action-mappings >
<action name= "Uploadsform" type= "com.cnehu.struts.action.UpLoadAction" path= "/uploadsaction" >
<forward name= "Display" path= "/display.jsp"/>
</action>
</action-mappings>
</struts-config>
Complete the full text!