当前位置: 首页 > news >正文

鞋子网站模板/百度网盘破解版

鞋子网站模板,百度网盘破解版,网站开发容易找工作吗,wordpress主题极简cho对于JSP的知识总结,已经为大家分享的差不多了,本篇就为大家做一下最后总结,本篇总结的主题是文件上传和下载,在之前的随笔中也有为大家分享过文件上传和下载的知识,不过都是通过JAVA提供的上传下载类实现的&#xff0c…

  对于JSP的知识总结,已经为大家分享的差不多了,本篇就为大家做一下最后总结,本篇总结的主题是文件上传和下载,在之前的随笔中也有为大家分享过文件上传和下载的知识,不过都是通过JAVA提供的上传下载类实现的,本篇将通过文件输入输出流的方式为大家精讲一下文件的上传和下载实现,我相信当你了解了底层的实现后,会很方便你对于文件的上传和下载进行拓展。好了废话不多说,下面我们开始本篇的总结。

 1、上传分析:

  文件上传就是,我们通过Form表单中的input属性,向后台发送用户需要上传的文件,后台当发现用户发送的请求后,首先将用户上传的文件保存到一个临时文件中,为接下来我们获取上传文件名和上传文件内容做准备。

 2、jsp页面要求:

  form的method="post"一定要设置为post方式,因为get方式传输,首先他是一种不安全的传输,其次它传输的内容大小有限,不能传输大量的数据。然后我们要修改enctype="multipart/form-data",默认值为:application/x-www-form-urlencoded。具体的代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>上传文件</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><center><h1>文件上传</h1><form action="<%=request.getContextPath() %>/servlet/ShangChuan" method="post" enctype="multipart/form-data">选择上传文件:<input type="file" name="file" id="file"/><input type="submit" value="上传"><span style="color: red">${news }</span></form></center></body>
</html>

 3、后台servlet:

  第一步:把用户上传的文件保存的一个临时文件

     InputStream fileSource = request.getInputStream();String tempFileName = "F:/cnblogs/text";File tempFile = new File(tempFileName);FileOutputStream outputStream = new FileOutputStream(tempFile);byte [] b = new byte[2048];int n;while((n = fileSource.read(b))!=-1){outputStream.write(b, 0, n);}//关闭输入、输出流
        outputStream.flush();outputStream.close();fileSource.close();

 

  第二步:通过获取临时文件中的内容来捕获用户上传文件的名字和内容区域的位置

//获取上传文件的名称RandomAccessFile randomFile = new RandomAccessFile(tempFile, "r");randomFile.readLine();String str = randomFile.readLine();int beginIndex = str.lastIndexOf("=\"")+2;int endIndex = str.lastIndexOf("\"");String fileName = str.substring(beginIndex, endIndex);System.out.println("fileName:"+fileName);//重新定位文件指针到文件头randomFile.seek(0);long startPosition = 0;int i = 1;//获取文件内容开始位置while((n=randomFile.readByte())!=-1&&i<=4){if(n=='\n'){startPosition = randomFile.getFilePointer();i++;}}startPosition = startPosition-1;//获取文件内容到结束位置
        randomFile.seek(randomFile.length());long endPosition = randomFile.getFilePointer();int j = 1;while(endPosition>=0&&j<=2){endPosition--;randomFile.seek(endPosition);if(randomFile.readByte()=='\n'){j++;}}endPosition = endPosition-1;//设置保存文件的路径String realPath = getServletContext().getRealPath("/")+"images";File fileupload = new File(realPath);if(!fileupload.exists()){fileupload.mkdir();}File saveFile = new File(realPath, fileName);RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile, "rw");//从临时文件当中读取文件内容(根据起止位置获取)
        randomFile.seek(startPosition);while(startPosition<endPosition){randomAccessFile.write(randomFile.readByte());startPosition = randomFile.getFilePointer();}//关闭输入、输出流,删除临时文件
        randomAccessFile.close();randomFile.close();tempFile.delete();request.setAttribute("news", "文件上传成功");request.getRequestDispatcher("/index.jsp").forward(request, response);

  这里是通过String类的LastIndexOf()方法和subString()方法来获得我们需要的内容。到这里我们的上传就实现了,下面我们来一起学习一下下载的工程。

 4、下载的JSP页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>文件下载</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><center><h1>文件下载操作</h1><a href="<%=request.getContextPath() %>/servlet/download?filename=text.txt" >Text.txt</a><span style="color: red">${news }</span></center></body>
</html>

 5、后台servlet:

public class download extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//获取文件下载路径String path = getServletContext().getRealPath("/")+"images/";String filename = request.getParameter("filename");File file = new File(path+filename);if(file.exists()){//设置相应的类型response.setContentType("application/x-msdownload");//设置头信息response.setHeader("content-Disposition", "attachment;filename=\""+filename+"\"");InputStream input = new FileInputStream(file);ServletOutputStream output = response.getOutputStream();byte[] b = new byte[2048];int n;while((n=input.read(b))!=-1){output.write(b, 0, n);}//关闭流对象
            output.close();input.close();}else{request.setAttribute("news", "文件不存在,下载失败。");request.getRequestDispatcher("/MyJsp.jsp").forward(request, response);}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}

  这下我们的文件上传和下载已经实现了,当然这些都是最基本的东西,大家可以发挥自己脑力进行拓展,到今天关于JSP的总结为大家分享完毕了,大家如有疑问请留言讨论。

转载于:https://www.cnblogs.com/AndroidJotting/p/4377465.html

http://www.jmfq.cn/news/5228821.html

相关文章:

  • 学做网站从前端到后端/推广一手渠道
  • 西安网站建设培训/国际购物网站平台有哪些
  • 贵阳网站建设980包年秒搜科技Sa50/关键词优化策略
  • 龙岗企业网站制作公司/头条发布视频成功显示404
  • 有哪些可以做外链的网站/微信营销推广
  • 商城网站如何建设/企业网络推广的方法
  • 献县做网站价格/代写文案平台
  • 平阳门户网站建设/线下引流推广方法
  • wordpress css文件路径/seo知名公司
  • 建设网站证书查询/网络营销是网上销售吗
  • 杭州下沙网站建设/百度一下首页官网
  • php自建网站/网络营销推广公司
  • WordPress客户端/宁波seo外包服务平台
  • 网站搜索引擎推广方案/seo外链专员工作要求
  • 互联网情况下做企业网站的有点/北京快速优化排名
  • 医疗器械注册证/推广seo优化公司
  • 海南网站优化/产品推广文案范例
  • 什么网站做简历模板/淘宝关键词top排行榜
  • 长春营销型网站制作/武汉seo网站推广培训
  • web网站扫描/做国外网站
  • 做网站的硬件/百分百营销软件
  • 网站建设业务拓展/项目推广方案怎么写
  • app手机网站制作/google官网注册
  • 北京网站怎么做/今日新闻消息
  • 建设设计公司网站/google搜索引擎优化
  • 黑icp 网站建设/邮件营销
  • 物流网站建设公司哪家好/三明网站seo
  • 新闻网站建设意义/武汉新一轮疫情
  • 网站备案每年一次/760关键词排名查询
  • 如何将自己做的网站上传/互联网平台推广