最近在做Struts項目時遇到了上傳多個文件的問題
一
package ehu
import javax
import org
import org
import org
import org
import org
import org
/**
* <p>
* Title:UpLoadForm
* </p>
* <p>
* Copyright: Copyright (c)
* </p>
* @author techyang
* @version
*/
public class UpLoadForm extends ActionForm
{
public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED =
protected FormFile theFile;
protected FormFile theFile
public FormFile getTheFile()
{
return theFile;
}
public void setTheFile(FormFile theFile)
{
this
}
public ActionErrors validate(ActionMapping mapping
HttpServletRequest request)
{
ActionErrors errors = null;
//has the maximum length been exceeded?
Boolean maxLengthExceeded = (Boolean) request
if ((maxLengthExceeded != null) && (maxLengthExceeded
{
errors = new ActionErrors();
errors
}
return errors;
}
/**
* @return Returns the theFile
*/
public FormFile getTheFile
{
return theFile
}
/**
* @param theFile
*/
public void setTheFile
{
this
}
}
二
package ehu
import java
import javax
import org
import org
import ehu
/**
* <p>
* Title:UpLoadAction
* </p>
* <p>
* Copyright: Copyright (c)
* </p>
* @author techyang
* @version
*/
public class UpLoadAction extends Action
{
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
throws Exception
{
String encoding = request
if ((encoding != null) && (encoding
{
response
}
UpLoadForm theForm = (UpLoadForm) form;
FormFile file = theForm
FormFile file
try
{
/*
* 取當前系統路徑D:\Tomcat
*/
String filePath = this
InputStream stream = file
ByteArrayOutputStream baos = new ByteArrayOutputStream();
/*
* 建立一個上傳文件的輸出流 如果是linux系統請把UploadFiles後的
*/
OutputStream bos = new FileOutputStream(filePath +
//D:\Tomcat
/* System
System
request
+ file
int bytesRead =
byte[] buffer = new byte[
while ((bytesRead = stream
{
bos
}
bos
stream
InputStream stream
ByteArrayOutputStream baos
OutputStream bos
int bytesRead
byte[] buffer
int i=
while ((bytesRead
{
bos
}
bos
stream
} catch (Exception e)
{
System
}
return mapping
}
}
三
<%@ taglib uri=
<html:html>
<head>
<title>用Struts上傳文件</title>
</head>
<body>
<html:form action=
<html:file property=
<html:file property=
<html:submit/>
</html:form>
</body>
</html:html>
四
<?xml version=
<!DOCTYPE struts
<struts
<data
<form
<form
</form
<global
<global
</global
<action
<action name=
<forward name=
</action>
</action
</struts
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28148.html