SmartUpload:文件上传下载

1.需要的包SmartUpload.jar

 

2.上传

public void upLoad(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException  {
		 request.setCharacterEncoding("utf-8");
		 response.setCharacterEncoding("utf-8");
		 response.setContentType("html/text;charset=utf-8");
		 String path = request.getServletContext().getRealPath("/img");
		 
		 File fl = new File(path);
		 if(fl.exists()){
			 fl.mkdir();
		 }
	     SmartUpload su = new SmartUpload();
	     try {
			su.initialize(this.getServletConfig(), request, response);
			su.upload();
			int upcount = su.save(path);
			String userage = su.getRequest().getParameter("userage");
			String username = su.getRequest().getParameter("username");
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     
	     List list = new ArrayList();
	     File[] files =fl.listFiles();
	     
	    for (int i = 0; i < files.length; i++) {
			
	    	
	    	if(files[i].isDirectory())
	    		continue;
	    	list.add(files[i].getName());
	    	
		} 
	   
	    request.setAttribute("filelist",list);
	    request.getRequestDispatcher("../downupload.jsp").forward(request, response);;
	 }
	

3.下载

public void downLoad(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException  {
		 request.setCharacterEncoding("utf-8");
		 response.setCharacterEncoding("utf-8");
		 response.setContentType("html/text;charset=utf-8");
		 String path = request.getServletContext().getRealPath("/img");   
		
		 
		 SmartUpload su = new SmartUpload();
	     su.initialize(this.getServletConfig(), request, response);
	     String filename = request.getParameter("filename").toString();
	     
	     try {
			su.downloadFile(path+"/"+filename);
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

前台界面展示:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>

<form method="POST" ENCTYPE="multipart/form-data" action="servlet/UpDownload?op=upLoad">
          <input type="file" name="upfile"/> 
	      <span>昵称:</span>  <input type="text" name="username"/>   
	       <span>年龄:</span>  <input type="text" name="userage"/>   
	      <input type="submit" value="上传">  
       </form>
     -------------------<br>
     <c:forEach items="${filelist }" var="item">
      <a href="servlet/UpDownload?op=downLoad&filename=${item }"> <span>下载:${item }</span></a><br>
      
     </c:forEach>
     <!-- ----------------------------------------------------------------------------------- -->    
    <script>
        function changeImg(img){
            
            img.src = img.src+"&"+ new Date().toString();
        }
    </script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值