请教在MVC的struts结构下,怎样在ACTION里实现文件上传。。谢谢

blackfiles 2003-08-20 11:05:46
在struts结构下,一般都是通过ActionForm把客户端的参数送到Action里处理,返回。

现在我想做一个文件上传功能。。在org.apache.commons.fileupload的包中,是这样定义得到的List fileItems = diskFileUpload.parseRequest( request );直接通过
HttpServletRequest request得到的上传文件的。

那么对于struts结构,在ACTION里,应该怎样得到呢?我定义了一个ActionForm(不做任何代码),然后也直接在Action里,用List fileItems = diskFileUpload.parseRequest( request ),发现什么都没得到。。

请教,这里应该怎样坐呢??
...全文
45 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
swithgirl 2003-08-20
  • 打赏
  • 举报
回复
在ACTION FORM里有一个这样的类:FormFile,你可以用这个去做
Wkenny 2003-08-20
  • 打赏
  • 举报
回复
/**
* 以下是Action coding
*/
public class UploadAction extends Action {

public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {

if (form instanceof UploadForm) {

UploadForm theForm = (UploadForm) form;

//retrieve the text data
String text = theForm.getTheText();

//retrieve the file representation
FormFile file = theForm.getTheFile();

//retrieve the file name
String fileName= file.getFileName();

//retrieve the content type
String contentType = file.getContentType();

boolean writeFile = theForm.getWriteFile();

//retrieve the file size
String size = (file.getFileSize() + " bytes");

String data = null;

try {
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
if (!writeFile) {
//only write files out that are less than 1MB
if (file.getFileSize() < (4*1024000)) {

byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
baos.write(buffer, 0, bytesRead);
}
data = new String(baos.toByteArray());
}
else {
data = new String("The file is greater than 4MB, " +
" and has not been written to stream." +
" File Size: " + file.getFileSize() + " bytes. This is a" +
" limitation of this particular web application, hard-coded" +
" in org.apache.struts.webapp.upload.UploadAction");
}
}
else {
//write the file to the file specified
OutputStream bos = new FileOutputStream(theForm.getFilePath());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
data = "The file has been written to \"" + theForm.getFilePath() + "\"";
}
//close the stream
stream.close();
}
catch (FileNotFoundException fnfe) {
return null;
}
catch (IOException ioe) {
return null;
}

//place the data into the request for retrieval from display.jsp
request.setAttribute("text", text);
request.setAttribute("fileName", fileName);
request.setAttribute("contentType", contentType);
request.setAttribute("size", size);
request.setAttribute("data", data);

//destroy the temporary file created
file.destroy();

//return a forward to display.jsp
return mapping.findForward("display");
}

//this shouldn't happen in this example
return null;
}


}

/**
* 以下是Form coding
*/


import org.apache.struts.upload.FormFile;
import org.apache.struts.action.ActionForm;


/**
* This class is a placeholder for form values. In a multipart request, files are represented by
* set and get methods that use the class org.apache.struts.upload.FormFile, an interface with
* basic methods to retrieve file information. The actual structure of the FormFile is dependant
* on the underlying impelementation of multipart request handling. The default implementation
* that struts uses is org.apache.struts.upload.DiskMultipartRequestHandler.
*
* @author Mike Schachter
* @version $Revision: 1.3 $ $Date: 2001/04/14 12:54:09 $
*/

public class UploadForm extends ActionForm {

/**
* The value of the text the user has sent as form data
*/
protected String theText;

/**
* Whether or not to write to a file
*/
protected boolean writeFile;

/**
* The file that the user has uploaded
*/
protected FormFile theFile;

/**
* The file path to write to
*/
protected String filePath;



/**
* Retrieve the value of the text the user has sent as form data
*/
public String getTheText() {
return theText;
}

/**
* Set the value of the form data text
*/
public void setTheText(String theText) {
this.theText = theText;
}

/**
* Retrieve a representation of the file the user has uploaded
*/
public FormFile getTheFile() {
return theFile;
}

/**
* Set a representation of the file the user has uploaded
*/
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}

/**
* Set whether or not to write to a file
*/
public void setWriteFile(boolean writeFile) {
this.writeFile = writeFile;
}

/**
* Get whether or not to write to a file
*/
public boolean getWriteFile() {
return writeFile;
}

/**
* Set the path to write a file to
*/
public void setFilePath(String filePath) {
this.filePath = filePath;
}

/**
* Get the path to write a file to
*/
public String getFilePath() {
return filePath;
}

public void reset() {
writeFile = false;
}
}
geleisi 2003-08-20
  • 打赏
  • 举报
回复
关注
blackfiles 2003-08-20
  • 打赏
  • 举报
回复
我是参看CSDN里的http://www.csdn.net/develop/article/20/20364.shtm。

那在struts结构下,可以用文章里的方法么??好象文章里的方法更简单。。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧