做了一个上传下载的程序,还有很多地方需要改进的地方,还是先贴上来吧,以后在用到的时候做个参照:
上传:用到struts提供的FormFile类,以及html:file标签.在设计上传页面的时候有多个上传浏览框.我这回用了一个土方法在页面上进行了循环赋值,所以在ActionForm中FormFile定义成数组,不知道还有没有更好的方法.
java 代码
- public class UploadAction extends Action {
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- int totalFileSize = 0;
- List list = new ArrayList();
- // 在web.xml中读取系统上传默认路径
- String path = servlet.getInitParameter("file-upload");
- // 获取页面信息,得到上传文件的相关信息.
- UploadForm upform = (UploadForm) form;
- // FormFile是struts提供的处理上传的类
- FormFile[] file = upform.getFile();
- for (int i = 0; i < file.length; i++) {
- // 取得上传文件的文件名.
- String fileName = file[i].getFileName();
- // 取得上传文件的大小,转成字符串类型的目的是为了在页面上显示文件大小.
- String size = file[i].getFileSize()+"";
- totalFileSize += file[i].getFileSize();
- if (fileName.equals("") || fileName.equals(null)) {
- request.setAttribute("total",totalFileSize);
- request.setAttribute("filearraylist",list);
- return mapping.findForward("success");
- } else {
- // 主要方法,上传文件
- uploadFile(file[i], path, fileName, size, upform, request);
- FileInfo fi = new FileInfo();
- fi.setFileName(fileName);
- fi.setFileSize(size);
- list.add(fi);
- // 上传成功后弹出上传成功对话框,在这里加一个标志,在页面进行判断用.
- request.setAttribute("flags", true);
- // 取得文件上传路径
- File fileList = new File(path);
- // 取得所有文件名
- String[] files = fileList.list();
- // 将文件名放入request范围在页面显示用.
- request.setAttribute("fileList", files);
- }
- }
- request.setAttribute("total",totalFileSize);
- request.setAttribute("filearraylist",list);
- return mapping.findForward("success");
- }
- private void uploadFile(FormFile file, String path, String fileName,
- String size, UploadForm upform, HttpServletRequest request) {
- try {
- InputStream is = file.getInputStream();
- OutputStream os = new FileOutputStream(path + "/" + fileName);
- // 文件大小
- byte[] buffer = new byte[20480];
- while ((is.read(buffer, 0, buffer.length)) != -1) {
- os.write(buffer, 0, buffer.length);
- }
- os.flush();
- file.destroy();
- // 关闭流
- close(is, os);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void close(InputStream is, OutputStream os) throws IOException {
- is.close();
- os.close();
- }
- }
- public class UploadForm extends ActionForm {
- private FormFile[] file = new FormFile[4];
- private String fileName;
- private String size;
- public FormFile[] getFile() {
- return file;
- }
- public void setFile(FormFile[] file) {
- this.file = file;
- }
- public String getFileName() {
- return fileName;
- }
- public void setFileName(String fileName) {
- this.fileName = fileName;
- }
- public String getSize() {
- return size;
- }
- public void setSize(String size) {
- this.size = size;
- }
- }
upload页面:
jsp 代码
- <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
- <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
- <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
- <%@ page language="java" contentType="text/html; charset=GB2312"%>
- <logic:present name="flags">
- <script language="javascript" >alert("上传成功!")</script>
- </logic:present>
- <html:html>
- <head>
- <title>test struts upload file</title>
- </head>
- <body>
- <html:errors/>
- <html:messages id="messages" message="true">
- <LI><bean:write name="messages" /></LI>
- </html:messages>
- <html:form action="upload" enctype="multipart/form-data">
- <table cellpadding="0" cellspacing="0" border="0" >
- <tr>
- <td>
- <table height="80%" width="80%">
- <tr>
- <td bgcolor="#ddcc">
- 选择文件
- </td>
- </tr>
- </table>
- </td>
- </tr>
- <tr height="50">
- <td width=2%>
- <table height="80%" width="80%" cellpadding="0" cellspacing="0" border="0">
- <tr>
- <% for(int i=0;i<4;i++){%>
- <td>
- <html:file property='<%="file["+i+"]"%>' />
- </td>
- <%
- }
- %>
- </tr>
- </table>
- </td>
- </tr>
- <tr>
- <td>
- <html:submit onclick="return(confirm('你确认要上传文件吗?'))">上传文件</html:submit>
- </td>
- </tr>
- <tr>
- <td>
- <table height="80%" width="80%">
- <tr bgcolor="#ddcc">
- <td width="50%">文件名</td>
- <td width="50%">文件大小(byte)</td>
- </tr>
- <tr>
- <logic:empty name="filearraylist" scope="request">
- <tr><td width="50%">总计</td><td width="50%">0</td></tr>
- </logic:empty>
- <logic:notEmpty name="filearraylist" scope="request">
- <logic:iterate id="element" name="filearraylist" scope="request">
- <tr>
- <td width="50%"><bean:write name="element" property="fileName" /></td>
- <td width="50%"><bean:write name="element" property="fileSize" /></td>
- </tr>
- </logic:iterate>
- <tr><td>总计</td><td><bean:write name="total"/></td></tr>
- </logic:notEmpty>
- </tr>
- </table>
- </td>
- </tr>
- </table>
- </html:form>
- <br/><br/><br/>
- <%int i=0;%>
- <logic:notEmpty name="fileList" scope="request">
- <table cellpadding="0" cellspacing="0" border="0">
- <tr>
- <td>
- <logic:iterate id="element" name="fileList" scope="request">
- <%= ++i%>
- <a href="<html:rewrite page='/downLoad.do' />?filename=<bean:write name='element' />"><bean:write name="element" /></a><br/>
- </logic:iterate>
- </td>
- </tr>
- </table>
- </logic:notEmpty>
- </body>
- </html:html>