`
mmmzzc
  • 浏览: 109855 次
  • 性别: Icon_minigender_1
  • 来自: 黑龙江
社区版块
存档分类
最新评论

struts2 上传下载

阅读更多
上传.jsp 部分代码  
<td >上传文件路径:</td>
<td >
  <s:file name="[color=brown]file[/color]"  id="Jiafile"/>
</td>
    

// 简单判断上传路径是否有效    form的name=“forms”
function sub(){
	   var  filespec =  document.forms[0].file.value;
	   var patrn=/^[C|D|E|F]:\\.+$/; 
		if (!patrn.exec(filespec)){
		   alert( "上传路径不正确!"); 
		   return  ;
		}    else{
                  document.forms[0].action="upload.action";
		document.forms[0].submit();
		}
}



uploadAction.java

private java.io.File  file;  //file与jsp中file的name相同
private String fileFileName;  // file的属性 上传文件的名字
private String path="/upload"; // WebRoot下文件夹upload
 
public String upload() {

//上传到文件夹中的名字 防止文件名重复 覆盖
String xname=UUID.randomUUID().toString()+fileFileName.substring( fileFileName.lastIndexOf("."));

//数据存库
.....

//上传到相应的文件夹
this.send(file, xname );	
 
 return SUCCESS; 
}

/**
* name 上传到文件夹中的 附件的名称
*/
public void  send( File  file , String name) throws Exception {

//以服务器的文件保存地址和原文件名建立上传文件输出流 
FileOutputStream fos = new FileOutputStream( 
	ServletActionContext.getServletContext().getRealPath(path) + "\\" + name);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
   {
	fos.write(buffer , 0 , len);
   }
	 fos.close(); 
	 fis.close();

}


下载
        
 private String inputPath ;
	private String filename;
	private String id ;
 

public InputStream getInputStream() throws Exception { 
   return  ServletActionContext.getServletContext().getResourceAsStream(inputPath);
	}
	  
public String downloadFile() throws IOException {  
 		FileManager service = new FileManager(this.getLoginId());   
 		cn.com.harbor.oa.file.entity.File  oaFile = service.getFileById(id);  
 		oaFile.setNumOfDownload(oaFile.getNumOfDownload() + 1);
 	  	service.update(oaFile); 
 	  	
 		inputPath="/user/file/upload/"+oaFile.getXname();//要下载的文件名称
        this.filename= new String( oaFile.getName().getBytes("GBK"),"ISO8859-1"); //保存文件时的名称 
        
        HttpServletResponse response =  ServletActionContext.getResponse();
return null;
}


	<action name="downloadFile" class="cn.com.harbor.oa.file.FileUploadAction"  method="downloadFile">
			 <result name="success" type="stream">
                <!--  param name="contentType">${contentType}</param-->
                 <param name="inputName">inputStream</param>                
                <param name="bufferSize">4096</param>
                <param name="contentDisposition">attachment;filename="${filename}"</param>
                <param name="">/user/file/fileUpload.jsp</param>
            </result>
			 
		</action>


//删除
/**
 * 删除文件夹内容
 */
	public boolean delPic( String xname )
    {    
			File file = new File(ServletActionContext.getServletContext().getRealPath(path)+"/"+xname);
			if(!file.exists()) { 
				return false;
			} else {
				if(file.exists() && file.isFile()) return file.delete(); 
				else return false;
			} 
      }



//下载取消报错
<constant name="struts.multipart.maxSize" value="9000000"/>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics