java

位置:IT落伍者 >> java >> 浏览文章

分享:用Struts上传多个文件的方法


发布日期:2019年12月17日
 
分享:用Struts上传多个文件的方法

最近在做Struts项目时遇到了上传多个文件的问题在网上查了不少资料也没有找到用Struts上传多个文件的例子我经过几天的研究实现了用Struts上传多个文件的功能现在贴出来让大家共享!

建立ActionForm

package ehustrutsform;

import javaxservlethttpHttpServletRequest;

import orgapachestrutsactionActionError;

import orgapachestrutsactionActionErrors;

import orgapachestrutsactionActionForm;

import orgapachestrutsactionActionMapping;

import orgapachestrutsuploadFormFile;

import orgapachestrutsuploadMultipartRequestHandler;

/**

* <p>

* Title:UpLoadForm

* </p>

* <p>

* Copyright: Copyright (c) techyang

* </p>

* @author techyang

* @version

*/

public class UpLoadForm extends ActionForm

{

public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = orgapachestrutswebappuploadMaxLengthExceeded;

protected FormFile theFile;

protected FormFile theFile;

public FormFile getTheFile()

{

return theFile;

}

public void setTheFile(FormFile theFile)

{

thistheFile = theFile;

}

public ActionErrors validate(ActionMapping mapping

HttpServletRequest request)

{

ActionErrors errors = null;

//has the maximum length been exceeded?

Boolean maxLengthExceeded = (Boolean) request

getAttribute(MultipartRequestHandlerATTRIBUTE_MAX_LENGTH_EXCEEDED);

if ((maxLengthExceeded != null) && (maxLengthExceededbooleanValue()))

{

errors = new ActionErrors();

errorsadd(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED new ActionError(

maxLengthExceeded));

}

return errors;

}

/**

* @return Returns the theFile

*/

public FormFile getTheFile()

{

return theFile;

}

/**

* @param theFile The theFile to set

*/

public void setTheFile(FormFile theFile)

{

thistheFile = theFile;

}

}

建立ActionServlet

package ehustrutsaction;

import javaio*;

import javaxservlethttp*;

import orgapachestrutsaction*;

import orgapachestrutsuploadFormFile;

import ehustrutsformUpLoadForm;

/**

* <p>

* Title:UpLoadAction

* </p>

* <p>

* Copyright: Copyright (c) techyang

* </p>

* @author techyang

* @version

*/

public class UpLoadAction extends Action

{

public ActionForward execute(ActionMapping mapping ActionForm form

HttpServletRequest request HttpServletResponse response)

throws Exception

{

String encoding = requestgetCharacterEncoding();

if ((encoding != null) && (encodingequalsIgnoreCase(utf)))

{

responsesetContentType(text/html; charset=gb);//如果没有指定编码编码格式为gb

}

UpLoadForm theForm = (UpLoadForm) form;

FormFile file = theFormgetTheFile();//取得上传的文件

FormFile file=theFormgetTheFile();

try

{

/*

* 取当前系统路径D:\Tomcat\webapps\coka\ 其中coka 为当前context

*/

String filePath = thisgetServlet()getServletContext()

getRealPath(/);

InputStream stream = filegetInputStream();//把文件读入

ByteArrayOutputStream baos = new ByteArrayOutputStream();

/*

* 建立一个上传文件的输出流 如果是linux系统请把UploadFiles后的\\换成/

*/

OutputStream bos = new FileOutputStream(filePath +

UploadFiles\\+filegetFileName());

//D:\Tomcat\webapps\coka\UploadFiles\DSCJPG

/*Systemoutprintln(filePath +

UploadFiles\\+filegetFileName());

Systemoutprintln(filePath);*/

requestsetAttribute(fileNamefilePath + /

+ filegetFileName());

int bytesRead = ;

byte[] buffer = new byte[];

while ((bytesRead = streamread(buffer )) != )

{

boswrite(buffer bytesRead);//将文件写入服务器

}

bosclose();

streamclose();

InputStream stream = filegetInputStream();//把文件读入

ByteArrayOutputStream baos = new ByteArrayOutputStream();

OutputStream bos =new FileOutputStream(filePath +

UploadFiles\\+filegetFileName());//建立一个上传文件的输出流

int bytesRead = ;

byte[] buffer = new byte[];

int i=;

while ((bytesRead = streamread(buffer )) != )

{

boswrite(buffer bytesRead);//将文件写入服务器

}

bosclose();

streamclose();

} catch (Exception e)

{

Systemerrprint(e);

}

return mappingfindForward(display);

}

}

建立上传用的JSP文件 uploadjsp

<%@ taglib uri=html prefix=html %>

<html:html>

<head>

<title>用Struts上传文件</title>

</head>

<body>

<html:form action=/uploadsAction enctype=multipart/formdata>

<html:file property=theFile/>

<html:file property=theFile/>

<html:submit/>

</html:form>

</body>

</html:html>

配置strutsconfigxml文件

<?xml version= encoding=UTF?>

<!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>

<strutsconfig>

<datasources />

<formbeans >

<formbean name=uploadsForm type=ehustrutsformUpLoadForm />

</formbeans>

<globalexceptions />

<globalforwards >

</globalforwards>

<actionmappings >

<action name=uploadsForm type=ehustrutsactionUpLoadAction path=/uploadsAction>

<forward name=display path=/displayjsp />

</action>

</actionmappings>

</strutsconfig>               

上一篇:spring用annotation-autowire配置

下一篇:Spring配置Hessian