[align=center][color=red][size=large][b]struts2文件上传详解[/b][/size][/color][/align]
[color=blue][b]一、jsp代码[/b][/color]
[color=blue][b]二、java代码[/b][/color]
[color=blue][b]三、struts配置[/b][/color]
[color=blue][b]
备注:[/b][/color]
[color=blue][b]一、jsp代码[/b][/color]
<form action="" method="post" enctype="multipart/form-data" >
<input name="upload" id="upload" type="file" class="input5" />
</form>
[color=blue][b]二、java代码[/b][/color]
package com.patronli.action;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.patronli.util.LogWriter;
public class testAction {
public void doUpload() throws IOException {
OutputStream os = null;
InputStream is = null;
try {
// 根据用户前台传递的地址,找到文件地址
is = new FileInputStream(upload);
// 指定文件上传到服务器哪个位置
os = new FileOutputStream("C:\\patronli\\" + orderNo + ".xls");
byte[] buffer = new byte[1024];
int len = 0;
try {
// 开始写入文件
while ((len = is.read(buffer)) > 0) {
os.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
LogWriter.debug("数据上传失败");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (is != null) {
is.close();
}
if (os != null) {
is.close();
}
}
}
}
[color=blue][b]三、struts配置[/b][/color]
<constant name="struts.multipart.maxSize" value="52428800" />
[color=blue][b]
备注:[/b][/color]
enctype这个属性管理的是表单的MIME编码。共有三个值可选:
application/x-www-form-urlencoded//表单默认MIME编码
multipart/form-data//传输数据的特殊类型编码(图片、文档等..)
text/plain//纯文本传输编码(邮件等..)