1.jsp页面
.type为file,name为file1
<form action="Actionmfm!upload.do" enctype="multipart/form-data"
method="post">
<table width="99%" border="0" align="center" cellpadding="0"
cellspacing="0">
<tr>
<td bgcolor="#D2D2A6">
<table width="100%" border="0" cellspacing="1" cellpadding="5">
<tr bgcolor="#EFEFEF">
<td colspan="4" class="fontC9b">
<div align="left" class="font12">
[::会议文件共享::]
<font color="red">可以在此上传自己的文件</font>
</div>
</td>
</tr>
<tr>
<td bgcolor="#EAF6EA">
<div align="right">
上传文件:
</div>
</td>
<td bgcolor="#FFFFFF">
<input type="file" name="file1" id="file1">
</td>
</tr>
<tr>
<td bgcolor="#EAF6EA">
<div align="right">
文件描述:
</div>
</td>
<td bgcolor="#FFFFFF">
<textarea name="fileDesc" id="fileDesc"></textarea>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
2.java上传方法
然后是action里的上传方法。以流的形式上传后把记录插入到数据库中。在这个类中,首先struts2获得文件名称。注意:因为file的名字为file1,所以这里的上传文件的名字为file1FileName。
private File file1; // 具体上传文件的 引用 , 指向临时目录中的临时文件
private String file1FileName; // 上传文件的名字 ,FileName 固定的写法
public File getFile1() {
return file1;
}
public void setFile1(File file1) {
this.file1 = file1;
}
public String getFile1FileName() {
return file1FileName;
}
public void setFile1FileName(String file1FileName) {
this.file1FileName = file1FileName;
}
然后才是上传方法
public String upload() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
// 输出流
OutputStream os = null;
// 输入流
InputStream is = null;
try {
// 获取文件存储路径
String path = ServletActionContext.getRequest().getSession()
.getServletContext().getRealPath("/");
File file = new File(path, "files");
if (!file.exists()) {
file.mkdirs();
}
String newPath = path + "files";
System.out.println("newPath" + newPath);
System.out.println("mid" + this.mId);
System.out.println("------------------------" + file1FileName);
/**
* 上传
*/
os = new FileOutputStream(new File(newPath, file1FileName));
is = new FileInputStream(file1);
// 上传大小控制(与xml中struts.multipart.maxSize值相同)
if (209000000 < is.available()) {
System.out.println(is.available());
request.setAttribute("MSG", "文件太大,上传失败");
return "uploadfail";
}
byte[] buf = new byte[1024];
int length = 0;
System.out.println(is.available());
while (-1 != (length = is.read(buf))) {
os.write(buf, 0, length);
}
/**
* 插入数据到数据库
*/
// 获取userid
User_info u = (User_info) ActionContext.getContext().getSession()
.get("userInfo");
int userId = u.getUserid();
String userAccount = u.getAccount();
String savePath = path + "files\\" + file1FileName;
System.out.println(savePath);
MeetingFileManageDto mfm = new MeetingFileManageDto(this.mId,
file1FileName, fileDesc, userId, savePath, 0, 2);
this.meetingFileManageService.insert(mfm);
System.out
.println("上传成功--------------------------------------------------");
this.MSG = "上传成功";
// 记录日志
InetAddress localHostAddress = null;
try {
localHostAddress = InetAddress.getLocalHost();
String ip = localHostAddress.toString();
new SystemLogService().addAllLog(userAccount, ip, "会议资料",
"上传会议资料");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
request.setAttribute("MSG", "上传失败");
e.printStackTrace();
return "uploadfail";
} finally {
is.close();
os.close();
}
return "uploadok";
}
3.配置
struts里的配置如下
<action name="Actionmfm!*" class="com.hb.action.meeting.MeetingFileManageAction"
method="{1}">
<result name="getAllMFforadmin">meeting/listmeetingfileforadmin.jsp
</result>
<result name="getAllMF">meeting/listmeetingfile.jsp
</result>
<result name="getAllMFforuser">meeting/listmeetingfileforuser.jsp
</result>
<result name="listfilemanage">meeting/listmeetingfile.jsp
</result>
<result name="meetinginfolist">meeting/listmeeting.jsp
</result>
<result name="uploadok" type="redirect">Actionmfm!getAllMF.do
</result>
<result name="showfour">frame/index.jsp
</result>
<result name="examine" type="redirect">Actionmfm!getAllMF.do
</result>
<result name="fresh">remind.jsp
</result>
<result name="remindjson" type="stream">
<param name="contentType">text/json</param>
<param name="inputName">inputStream</param>
</result>
</action>
还有struts中对文件大小的控制
<constant name="struts.multipart.maxSize" value="20971520" />