strust1 上传文件要点

1. private transient FormFile formFile; 

2.String license = new String(Util.getFile(licenseManageForm.getFormFile()),UTF8_STRING); 

3. Util.getFile

/**

* @param file

* @return

*/

public static byte[] getFile(FormFile file){

if(null == file)

return new byte[0];

InputStream stream = null;

try{

stream = file.getInputStream();

    byte[] readBuffer = new byte[0];

   

    //read

    byte[] buffer = new byte[READ_LENGTH];

    int readNum = -1;

    while ((readNum = stream.read(buffer, 0, READ_LENGTH)) != -1) {

    readBuffer = uniteByteArrays(readBuffer, buffer, readNum);

    }

    return readBuffer;

   

}catch(IOException ex){

log.error("", ex);

return new byte[0];

}finally{

try{

if(null != stream)

stream.close();

}catch(IOException ex){

log.error("", ex);

}

}

}

4.

jsp

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<html:form action="LicenseManage.do?method=readLicenseFile" onsubmit="return checkFile();"

method="post" enctype="multipart/form-data" target="hidden_frame">

<input type="file" name="formFile" contentEditable="false"/>

</html:form>

猜你喜欢

转载自wwyu8901.iteye.com/blog/1463994