关于Struts中文件的上传
2007-06-26 17:10
351 查看
①实现原理:
把上传的文件作为一个输入流对象读入,再写到一个输出流对象中,
从而生成一个和上传文件一样的文件,原理和文件的复制类似。
②上传通用函数Action:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts.upload.FormFile;
protected void uploadFile( UploadForm form, String target ) throws exception {
FormFile file = form.getTheFile();
InputStream stream = null;
OutputStream bos = null;
try {
stream = file.getInputStream();
bos = new FileOutputStream( target );
int bytesRead = 0;
byte[] buffer = new byte[ 1024 ];
while ( ( bytesRead = stream.read( buffer, 0, 1024 ) ) != -1 ) {
bos.write( buffer, 0, bytesRead );
}
} catch ( FileNotFoundException fnfe ) {
throw new exception( "" );
} catch ( IOException ioe ) {
throw new exception( "" );
} finally {
try {
if ( stream != null ) {
stream.close();
}
if ( bos != null ) {
bos.close();
}
} catch ( IOException e ) {
throw new exception( "" );
}
}
file.destroy();
}
③上传Action:
try {
this.uploadFile( 上传Form, 文件上传位置全路径 );
} catch ( exception e ) {
}
注意上传Action必须继承上传通用函数Action。
④上传通用Form:
import org.apache.struts.upload.FormFile;
public class UploadForm extends PageForm {
private FormFile theFile;(及其getter和setter)
}
⑤上传jsp:
使用<html:file property="theFile" />控件,注意上传Form必须继承上传通用Form。
把上传的文件作为一个输入流对象读入,再写到一个输出流对象中,
从而生成一个和上传文件一样的文件,原理和文件的复制类似。
②上传通用函数Action:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts.upload.FormFile;
protected void uploadFile( UploadForm form, String target ) throws exception {
FormFile file = form.getTheFile();
InputStream stream = null;
OutputStream bos = null;
try {
stream = file.getInputStream();
bos = new FileOutputStream( target );
int bytesRead = 0;
byte[] buffer = new byte[ 1024 ];
while ( ( bytesRead = stream.read( buffer, 0, 1024 ) ) != -1 ) {
bos.write( buffer, 0, bytesRead );
}
} catch ( FileNotFoundException fnfe ) {
throw new exception( "" );
} catch ( IOException ioe ) {
throw new exception( "" );
} finally {
try {
if ( stream != null ) {
stream.close();
}
if ( bos != null ) {
bos.close();
}
} catch ( IOException e ) {
throw new exception( "" );
}
}
file.destroy();
}
③上传Action:
try {
this.uploadFile( 上传Form, 文件上传位置全路径 );
} catch ( exception e ) {
}
注意上传Action必须继承上传通用函数Action。
④上传通用Form:
import org.apache.struts.upload.FormFile;
public class UploadForm extends PageForm {
private FormFile theFile;(及其getter和setter)
}
⑤上传jsp:
使用<html:file property="theFile" />控件,注意上传Form必须继承上传通用Form。
相关文章推荐
- 一个关于用Struts实现文件上传异常的解决
- 关于Struts 2文件上传报错:Unable to find 'struts.multipart.saveDir' property setting
- 关于文件上传,我要向struts提点意见
- 帮帮忙了,我是初学者在学习Struts2时关于文件上传时的错误,不知道怎么去改了,让我花费一下午的时间还是不对,一直提示我配置不对
- 关于struts文件上传遇到只有文件值没有接收到以及文件消失的解决方案
- 关于Struts2文件上传Unable to find 'struts.multipart.saveDir'错误的解决
- 关于Struts多文件上传的小例子--可完全实现
- 关于ssi-uploader在strutsMVC的单文件/多文件上传实现
- 一个关于用Struts实现文件上传异常的解决
- 关于Struts 2文件上传报错:Unable to find 'struts.multipart.saveDir' property setting
- 关于Struts 2文件上传报错:Unable to find 'struts.multipart.saveDir' property setting
- 使用Struts 2框架上传文件
- HttpClient4.4.1 关于文件上传 中文文件名编码问题
- struts2多文件上传
- 关于文件上传的一些笔记
- struts2 多文件上传与下载
- struts多文件上传案例演示(一)
- 如何修改struts上传文件大小
- 关于文件上传组件国内外完美解决方案的调查
- struts2.X心得8--struts与servlet的松耦合scope之间的访问以及文件上传