struts2附件上传下载管理

jsp代码:

 

<form action="upload.action" method="post" enctype="multipart/form-data">   
<table>   
<tbody>   
选择第一个文件:<input type="file" name="upload" /><br>   
选择第二个文件:<input type="file" name="upload" /><br>   
选择第三个文件:<input type="file" name="upload" /><br>  
<input value="上传" type="submit" />   
</tbody>   
</table>   
</form>

<table cellspacing="1" id="roleTable" class="tablesorter" style="width:98%" >					
		<thead>
			<tr>
			  <th  width="30"><input type="checkbox" onClick="checkallRows(this)" id="checkall"></input></th> 
			  <th  width="20"></th>
			  <th>文件名称</th>  
			  <th>上传人员</th>
	          <th>文件大小</th>
	          <th>上传时间</th>
	          <th></th>
	          <th></th>                        
	        </tr>
	      </thead>
	      <tbody>
	        <s:iterator  value="attachmentList" id="attachment" status="tpls">
		      <tr >
	          <td><input type="checkbox"  name="cName" value="${staffId}"/></td>
	          <td><a href="#" class="expand-data-more"></a></td><!--排序按钮 需要展开必加 -->
	          <td>${attachment.fileName}</td>
	          <td>${attachment.creater}</td> 
	          <td>${(attachment.fileSize - attachment.fileSize%1024)/1024} KB</td>
	          <td>${attachment.createTime}</td>
	          <td><a  href="download.action?downloadFileName=${attachment.fileName}&downloadFilePath=${attachment.filePath}">
		    					<span><span class="add"></span>下载</span></a></td>
		      <td><a  href="deleteUpload.action?id=${id}&downloadFilePath=${attachment.filePath}">
		      					<span><span class="add"></span>删除</span></a></td>
		      <!------------END TD----------------------------->
	        </tr>
		   	</s:iterator>
		  </tbody>
	 </table>

 java代码:

 

public List<Attachment> attachmentList;

/**
     * 上传
     * @return
     * @throws Exception
     */
    public String upload() throws Exception
    { 
    	//getUpload()是临时文件
        List<File> files = getUpload();
        
        String fileName = "";
        for (int i = 0 ; i < files.size() ; i++)
        {
        	//fileName= "c3p0-0.9.1.2.bin.zip"
        	fileName = getUploadFileName().get(i);
        	
        	Attachment a = new Attachment();
        	a.setFileName(fileName);
        	
        	String filetype = fileName.substring(fileName.lastIndexOf("."));
        	//fileurl= "20101228181435906.zip"
    		String fileurl = DateCl.getDateToFilename() + filetype;
    		 
        	a.setCreateTime(DateCl.getFullDayTime());
        	 
        	//D:\Tomcat 6.0\webapps\bip-platform\\upload\yyyy-MM-dd
        	File path = new File(getSavePath() + "\\" + DateCl.getDay());
        	
            boolean bFile  = path.exists();
            if( bFile == false ){
            	bFile = path.mkdir();
            }
        	 
        	a.setFilePath(path.getPath() + "\\" + fileurl);
            FileOutputStream fos = new FileOutputStream(path.getPath() + "\\" + fileurl);
            FileInputStream fis = new FileInputStream(files.get(i));
            byte[] buffer = new byte[1024];
            int len = 0;
            long totalLen = 0;
            while ((len = fis.read(buffer)) > 0)
            {
            	totalLen += len;
                fos.write(buffer , 0 , len);
            }
            a.setFileSize(totalLen);
            jbpmAttachmentService.saveAttachmentPO(a);
        }
        return SUCCESS;
    }

 /**
     * 下载
     * @return
     * @throws Exception
     */
    public String download()throws Exception{
    	HttpServletRequest request = ServletActionContext.getRequest();
		downloadFilePath = (String)request.getParameter("downloadFilePath");
    	downloadFileName = (String)request.getParameter("downloadFileName");
    	downloadFilePath = fileUploadUtil.convertDownloadFilePath(getSavePath(), downloadFilePath);
    	convertDownloadFilePath();
    	return SUCCESS;
    }
    /**
     * 组织下载文件路径
     * @throws Exception 
     *
     */
    private void convertDownloadFilePath() throws Exception{
    	//得到这个路径upload\20101221190611093.zip  D:\Tomcat 6.0\webapps\bip-platform\\upload
    	String importPath = getSavePath();
    	importPath = importPath.substring(importPath.lastIndexOf("\\")+1, importPath.length());
    	downloadFilePath = downloadFilePath.substring(downloadFilePath.lastIndexOf(importPath), downloadFilePath.length());
    }
    public InputStream getTargetFile() throws Exception {
		InputStream is = ServletActionContext.getServletContext().getResourceAsStream(downloadFilePath);
		return is;
	}

    /**
     * 展示
     * @return
     */
    public String showUpload(){
    	attachmentList = jbpmAttachmentService.getAllAttachment();
    	return SUCCESS;
    }
    
    /**
     * 删除
     * @param upload
     */
    public String deleteUpload(){
//    	File deleteFile = new File(downloadFilePath);
//    	if(deleteFile != null){
//    		deleteFile.delete();
//    	}
    	HttpServletRequest request = ServletActionContext.getRequest();
		String id = (String)request.getParameter("id");
		jbpmAttachmentService.deleteAttachmentPO(Long.parseLong(id));
    	return SUCCESS;
    }

 struts_upload.xml文件代码:

 

<constant name="struts.multipart.maxSize" value="104857601"></constant>
	<constant name="struts.custom.i18n.resources" value="messageResource"/>
	
	
	
	<!--  附件管理-->
	<package name="upload" namespace="/uploadMgr"
		extends="struts-default">
		
		<!-- 显示上传页面 -->
		<action name="showUpload" class="uploadAction"
			method="showUpload">
			<result name="success">
				showUpload.jsp
			</result>
		</action>


		<!-- 上传附件 -->
		<action name="upload" class="uploadAction"
			method="upload">
			
			<interceptor-ref name="fileUpload"> 
                <!-- 定义允许的上传文件的类型-->
			<param name="allowedTypes">application/vnd.ms-excel</param>
                 <param name="allowedTypes">application/x-zip-compressed,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/zip,application/octet-stream</param>
                  <!--上传文件的最大值,超過100M,這裡就失去了作用   -->
                <param name="maximumSize">104857600</param>
            </interceptor-ref>  
             
            <!-- 默認拦截器 -->
            <interceptor-ref name="defaultStack"/>    
            
			<result name="success" type="chain">
				showUpload
			</result>
			<result name="input" >
				showUpload.jsp
			</result>
		</action>
		 
		 
		<!-- 下载 -->
		<action name="download" class="uploadAction"
			method="download">
			<result name="success" type="stream">
            	<param name="contentType">application/zip</param>
            	<param name="inputName">targetFile</param>
            	<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
            	<param name="bufferSize">4096</param>
            </result>    
		</action> 
		
		<!-- 删除附件 -->
		<action name="deleteUpload" class="uploadAction"
			method="deleteUpload">
			<result name="success" type="chain">
				showUpload
			</result>
		</action>

你可能感兴趣的:(tomcat,jsp,struts,Excel)