<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<s:form name="idFrmMain" method="post" enctype="multipart/form-data">
<table style="margin-left: 100px;" border=0 cellspacing=0 cellpadding=2 bordercolor="#FFFFFF" width="400">
<col span=1 width="3600">
<tr>
<th>上传文件</th>
<td>
<s:file name="file" size="40" οnkeypress="return false;" />
</td>
</tr>
<tr align="center">
<td>
<input type="button" style="width: 80px; height: 25" value="上传" οnclick="javascript:upload();">
</td>
</tr>
</table>
</s:form>
</body>
</html>
文件为file,文件名必须为file+FileName(即fileFileName),只有这样才能通过get和set方法自动获取到文件名。该文件名不包括文件的路径
表单中必须加enctype="multipart/form-data",它用来设置表单的MIME编码。默认情况,编码格式是application/x-www-form-urlencoded,不能用于文件上传;
只有使用了multipart/form-data,才能完整的传递文件数据.enctype="multipart/form-data"是上传二进制数据。
public class UploadForm{
/** 文件 */
private File file;
/** 文件名 */
private String fileFileName;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
}
public class UploadAction{
private UploadForm form;
public UploadForm getForm() {
return form;
}
public void setForm(UploadForm form) {
this.form = form;
}
/**
* <pre>
* 上传文件.
* </pre>
*
* @return 处理结果
* @throws Exception 异常
*/
public String upload() throws Exception {
if (form == null) {
form = new UploadForm();
}
String fileName = form.getFileFileName();
File fileObj = form.getFile();
String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");//上传服务器路径
uploadFile(uploadPath, fileName, FileIO.read(fileObj));//调用uploadFile()方法
return SUCCESS;
}
/**
* <pre>
* 执行文件的写入.
* </pre>
*
* @param path 路径
* @param fileName 文件名
* @param buffer 写入目标字符串
* @throws Exception 异常
*/
public static void uploadFile(String path, String fileName,StringBuffer buffer) throws Exception {
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
String fullFileName = path + File.separatorChar + fileName;
BufferedWriter bw = null;
try {
bw = getBufferedWriter(fullFileName, false);
bw.write(buffer.toString());
bw.flush();
} catch (FileNotFoundException e) {
throw new Exception("文件不存在");
} finally {
if (bw != null) {
bw.close();
}
}
}
/**
* <pre>
* 取得文件缓冲区输出流.
* </pre>
*
* @param fileName 文件名(包含路径)
* @param writeMode 写入模式(true:追加、false:覆盖)
* @return 缓冲区输出流
* @throws Exception 异常
*/
public static BufferedWriter getBufferedWriter(String fileName,boolean writeMode) throws Exception {
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream(fileName, writeMode);
osw = new OutputStreamWriter(fos, "ISO8859-1");
return new BufferedWriter(osw);
} catch (Exception e) {
try {
if (osw != null) {
osw.close();
} else if (fos != null) {
fos.close();
}
} catch (IOException ioException) {
throw new Exception("关闭时发生异常");
}
throw e;
}
}
/**
* <pre>
* 返回设定到读入文件的字符缓冲区.
* </pre>
*
* @param file 文件
* @return 文件的内容
* @throws Exception 异常
*/
public static StringBuffer read(File file) throws Exception {
StringBuffer buffer = null;
buffer = readFile(file);
return buffer;
}
/**
* <pre>
* 返回设定到读入用参数传递的文件(通常文件、临时文件)的字符缓冲流.
* </pre>
*
* @param file 文件
* @return 文件的内容
* @throws Exception 文件读入时的异常
*/
public static StringBuffer readFile(File file) throws Exception {
StringBuffer buffer = new StringBuffer();
BufferedReader inBuffer = null;
try {
inBuffer = getBufferedReader(file);
char[] chars = new char[2048];
int len;
while ((len = inBuffer.read(chars)) != -1) {
buffer.append(chars, 0, len);
}
} catch (FileNotFoundException e) {
throw new Exception("文件不存在");
} finally {
if (inBuffer != null) {
inBuffer.close();
}
}
return buffer;
}
}
调用js里的upload()函数
function upload() {
document.idFrmMain.action = "fileUpload.do";
document.idFrmMain.target = "_self";
document.idFrmMain.submit();
}
struts.xml
<action name="fileUpload" class="UploadAction" method="upload">
<result name="success">/jsp/success.jsp</result>
</action>