文件下载上传



1.文件下载

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Download extends HttpServlet {

 
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
   String filepath = this.getServletContext().getRealPath("download//呵呵.jpg");
   System.out.println(filepath);
   int index = filepath.indexOf("\\");
   String fileName = filepath.substring(index+1);
   response.setHeader("content-disposition","attachment;fileName="+URLEncoder.encode(fileName,"utf-8"));
   InputStream in = new FileInputStream(filepath);
   OutputStream out = response.getOutputStream();
   byte[] b = new byte[1024];
   int len = 0;
   while((len = in.read(b))!=-1){
    out.write(b, 0, len);
   }
   in.close();

 }

 
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  doGet(request,response);
 }

}

2 文件上传 运用 smartupload 组件


import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class RefactorUpload extends HttpServlet {

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  IpTimeNum iPTimeNum = new IpTimeNum(request.getRemoteAddr());
     SmartUpload su = new SmartUpload();
     su.initialize(this.getServletConfig(),request,response);
     try {
     String path = this.getServletContext().getRealPath("/")+"/file";
//      
   su.upload();
   Files file = su.getFiles(); // 获取文件组
   com.jspsmart.upload.File files=file.getFile(0);
   System.out.println(files.getFileExt());
   String fname =files.getFileName();
   //实例化文件对象
  String Filename  =iPTimeNum.getIPTimeRand()+fname;
  su.getFiles().getFile(0).saveAs(java.io.File.separator+"/file"+java.io.File.separator+Filename);
   String fileName = iPTimeNum.getIPTimeRand();
   response.getWriter().print("成功上传文件:"+fileName);
   
  } catch (SmartUploadException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  doGet(request, response);
 }

}

2.1  创建 upload.jsp 


 <form  action="RefactorUpload" method="post" enctype="multipart/form-data">
    <input type="text" name="name"/>
     <input type="file" name="file"/>
     <input type="submit" value="上传">
</form>

  </body>


3  文件下载 运用 smartupload组件

  1. Index.jsp

    <div align="center">

                   <A HREF="download1.jsp?path=<%=basePath+"file/011.jpg"%>">

                    <img src="file/download.GIF" width="22"height="22"border="0"></A>

     </div>

    2.download.jsp

    <%

       

             response.setCharacterEncoding("utf-8");

             String path=request.getParameter("path");

             //path=new String(path.getBytes("iso-8859-1"));

             String fileName = path.substring(path.lastIndexOf("file"));

             SmartUpload su = new SmartUpload(); // 新建一个smartupload对象    

             su.initialize(pageContext);          // 初始化准备操作 

             su.setContentDisposition(null);

            

             su.downloadFile(fileName);

             //解决异

             out.clear();

             out = pageContext.pushBody();

            

         %>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值