web上传下载

提示存储地址的下载

		public ActionForward exportExcel(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {  
                 String path;
		 //创建临时文件夹  
		 path=request.getContextPath()+"\\shopCart";  
		 new File(path).mkdirs();  
		   //创建临时文件,存放需要下载的内容  
		 path=path+"\\"+new Date().getTime()+".xls";  
		 File file = new File(path);  
		 //设置头信息,声明文件类型,文件默认名称
		 response.setContentType("application/msexcel");
		 response.setHeader("content-disposition", "attachment; filename=order_"+new Date().getTime()+".xls");
		 //读取临时文件内容,将内容写到客户端  
		 OutputStream os;  
		 try {  
		     os = response.getOutputStream();  
		     java.io.BufferedInputStream bis = new java.io.BufferedInputStream(new java.io.FileInputStream(file));  
		     byte[] readdata = new byte[1024];  
		     while (bis.read(readdata) != -1) {  
		         os.write(readdata);  
		     }  
		     os.flush();  
		     os.close();  
		     bis.close();  
		    //将临时文件删除  
		     file.delete();  
		     response.sendRedirect("/ecman/searchMerchandise.jsp");  
		 } catch (IOException e1) {  
		     e1.printStackTrace();  
		 }  
		 return null;  
 

 

基于struts 上传操作

首先請確定commons-fileupload.jar有在WEB-INF/lib目錄下。   

FormBean:                                                       
import   javax.servlet.http.*;   
import   org.apache.struts.action.*;   
import   org.apache.struts.upload.*;                                                         
public   class   UploadForm   extends   ActionForm   {   
        private   FormFile   file;                                                      
        public   void   setFile(FormFile   file)   {   
                this.file   =   file;   
        }                                                          
        public   FormFile   getFile()   {   
                return   file;   
        }                                                         
        public   void   reset(ActionMapping   mapping,   HttpServletRequest   req)   {   
                file   =   null;   
        }   
} 
使用struts及html标签
<html:form  action= "/cart.do" method= "post" enctype= "multipart/form-data">     
<html:hidden property="status" value="importExcel"/>
	 文件路径: <html:file property= "excelFile"/>     
	<input type="submit" value="上传"/>     
	</html:form>
<form action="/cart.do" method="post" enctype="multipart/form-data" name="excelForm">
	<input type="hidden" name="status" value="importExcel"/>
	文件路径:<input name="excelFile" type="file" id="excelFile"/>
	<input type="submit" name="Submit" value="提交"/>
</form>
   
 ction: 
import   java.io.*;   
import   javax.servlet.http.*;   
import   org.apache.struts.action.*;   
import   org.apache.struts.upload.*;   
public   class   UploadAction   extends   Action   {   
        public   ActionForward   execute(ActionMapping   mapping,   
                                                                  ActionForm   form,   
                                                                  HttpServletRequest   request,   
                                                                  HttpServletResponse   response)    throws   Exception   {   
                UploadForm   fileForm   =   (UploadForm)   form;   
                FormFile   file   =   fileForm.getFile();   
                FileOutputStream   fileOutput   =   new   FileOutputStream( "/home/caterpillar/files/ "   +   file.getFileName());                                                                                                                                                                        
                fileOutput.write(file.getFileData());   
                fileOutput.flush();   
                fileOutput.close();                                                                                                                                                                                           
                return   mapping.findForward( "success ");   
        }   
} 

 

你可能感兴趣的:(apache,Web,struts,servlet,OS)