欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Java文件輸入輸出流實(shí)現(xiàn)文件上傳下載功能

 更新時(shí)間:2018年04月26日 14:36:24   作者:糖拌西紅柿  
這篇文章主要為大家詳細(xì)介紹了基于Java文件輸入輸出流實(shí)現(xiàn)文件上傳下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了Java實(shí)現(xiàn)文件上傳下載功能的具體代碼,供大家參考,具體內(nèi)容如下

前端通過(guò)form表單的enctype屬性,將數(shù)據(jù)傳遞方式修改為二進(jìn)制”流“的形式,服務(wù)端(servlet)通過(guò)  getInputStream() 獲取流信息, 運(yùn)用java I/O 流的基礎(chǔ)操作將流寫(xiě)入到一個(gè)服務(wù)端臨時(shí)創(chuàng)建的文件temp中,然后再次利用文件基本操作,讀取并截取臨時(shí)文件內(nèi)容,根據(jù)其中信息創(chuàng)建相應(yīng)的文件,將讀取出來(lái)的具體信息寫(xiě)入,下載時(shí),根據(jù)提交的文件名稱(chēng),找到服務(wù)器端相應(yīng)的文件,然后根據(jù)輸出流outStream輸出到頁(yè)面,同時(shí)將servlet的響應(yīng)類(lèi)型和響應(yīng)頭進(jìn)行設(shè)置。

具體傳輸流程如下圖:

流信息的部分為:

具體代碼如下:

前端代碼:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script src="Js/jquery.js"></script>
</head>
<body>
 <form action="FileUpServlet" method="post" enctype="multipart/form-data">
 <table>
 <tr>
  <td>請(qǐng)選擇上傳文件:</td><td><input id="myfile" name="myfile" type="file" value="" /></td>
  <td><input type="submit" value="上傳"></td>
 </tr>
 <tr><td>${info}</td></tr>
 </table> 
 </form>
  文件下載:<a href="FileLoadownServlet?filename=${filename}">${filename}</a>
</body>
</html>

上傳servlet部分(核心)

@WebServlet("/FileUpServlet")
public class FileUpServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
  
 /**
  * @see HttpServlet#HttpServlet()
  */
 public FileUpServlet() {
  super();
  // TODO Auto-generated constructor stub
 }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  doPost(request, response);
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  request.setCharacterEncoding("utf-8");
  InputStream filesource = request.getInputStream();//request獲取流信息
  String tempname = "D:/temp";//tempfile代表臨時(shí)存放文件
  File tempfile = new File(tempname);//創(chuàng)建臨時(shí)文件
  
  FileOutputStream outputStream = new FileOutputStream(tempfile);//輸出流對(duì)象,指定輸出指tempfile目錄下
  byte b[] = new byte[1024];
  int n;
  while((n = filesource.read(b))!= -1)//從輸出流中每次讀取1024字節(jié),直至讀完
  {
   outputStream.write(b,0,n);
  }
  outputStream.close();
  filesource.close();//關(guān)閉輸入輸出流
  
  
  /*以下為具體的文件操作,主要為解析臨時(shí)產(chǎn)生的 temp文件 ,知識(shí)多為java輸入輸出流的內(nèi)容!*/
  

  RandomAccessFile randomfile = new RandomAccessFile(tempfile, "r");//隨機(jī)流,指定要讀臨時(shí)文件,只讀
  randomfile.readLine();//讀取第一行,無(wú)效數(shù)據(jù),不需要
  String str = randomfile.readLine();//讀取第二行
  int beginIndex = str.lastIndexOf("=")+2;//指定所需數(shù)據(jù)的開(kāi)始位置
  int endIndex = str.lastIndexOf("\"");//指定所需數(shù)據(jù)截至位置
  String filename = str.substring(beginIndex,endIndex);//截取文件名
  
  //重新定位文件指針,獲取文件內(nèi)容
  randomfile.seek(0);//文件指針從頭開(kāi)始
  long startext = 0;
  int i = 1;
  //文件內(nèi)容開(kāi)始位置
  while((n=randomfile.readByte()) != -1&&i <= 4)
  {
   if(n=='\n')
   {
    startext = randomfile.getFilePointer();
    i++;
   } 
  }
  startext = randomfile.getFilePointer() - 1;
  //獲取文件內(nèi)容 結(jié)束位置
  randomfile.seek(randomfile.length());
  long endtext = randomfile.getFilePointer();
  int j = 1;
  while(endtext >= 0 && j <= 2)
  {
   endtext--;
   randomfile.seek(endtext);
   if(randomfile.readByte()=='\n')
   {
    j++;
   }
  }
  endtext = endtext-1;
  
  //將臨時(shí)文件保存至指定目錄中 
  String realpath = getServletContext().getRealPath("/")+"images";//設(shè)置文件保存目錄
  System.out.println(realpath);
  File fileupload = new File(realpath);
  if(!fileupload.exists())
  {
   fileupload.mkdir();//目錄不存在則創(chuàng)建
  }
  File savefile = new File(realpath,filename);
  RandomAccessFile randomAccessFile = new RandomAccessFile(savefile, "rw");
  randomfile.seek(startext);
  while(startext<endtext){
   randomAccessFile.write(randomfile.readByte());//文件寫(xiě)入
   startext = randomfile.getFilePointer();
  }
  //關(guān)閉各種輸入輸出流
  randomAccessFile.close();
  randomfile.close();
  tempfile.delete();//刪除臨時(shí)文件
  
  
  SimpleDateFormat timed = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date nowdate = new Date();
  String time = timed.format(nowdate.getTime());
  request.setAttribute("info", time+" "+filename+"上傳成功!");
  request.setAttribute("filename", filename);
  request.getRequestDispatcher("/fildeOp.jsp").forward(request, response);
 }

}

下載部分

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  String filename = request.getParameter("filename");
  String path = getServletContext().getRealPath("/")+"images/";
  File file = new File(path+filename);//找到文件
  if(file.exists())
  {
   response.setContentType("application/x-msdownload"); //設(shè)置響應(yīng)類(lèi)型,此處為下載類(lèi)型
   response.setHeader("Content-Disposition", "attachment;filename=\""+filename+"\"");//以附件的形式打開(kāi)
   InputStream inputStream = new FileInputStream(file);
   ServletOutputStream outputStream = response.getOutputStream();
   byte b[] = new byte[1024];
   int n;
   while((n = inputStream.read(b)) != -1)
   {
    outputStream.write(b,0,n);
   }
   outputStream.close();
   inputStream.close();   
   }else{
   request.setAttribute("result", "文件不存在!下載失??!");
   request.getRequestDispatcher("/fildeOp.jsp").forward(request, response);
  }
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 談?wù)勛兞棵?guī)范的重要性

    談?wù)勛兞棵?guī)范的重要性

    下面小編就為大家?guī)?lái)一篇談?wù)勛兞棵?guī)范的重要性。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • Java的sort的排序及使用詳解

    Java的sort的排序及使用詳解

    這篇文章主要為大家詳細(xì)介紹了Java的sort的排序及使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 ,希望您能夠多多關(guān)注
    2022-02-02
  • Springboot集成fastDFS配置過(guò)程解析

    Springboot集成fastDFS配置過(guò)程解析

    這篇文章主要介紹了Springboot集成fastDFS配置過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Java 中的類(lèi)和對(duì)象詳情

    Java 中的類(lèi)和對(duì)象詳情

    這篇文章主要介紹了Java 中的類(lèi)和對(duì)象,類(lèi)可以看成是創(chuàng)建Java對(duì)象的模板,下面文章圍繞著Java 類(lèi)與對(duì)象詳細(xì)內(nèi)容展開(kāi),需要的朋友可以參考一下
    2021-11-11
  • SpringMVC修改返回值類(lèi)型后的消息轉(zhuǎn)換器處理方式

    SpringMVC修改返回值類(lèi)型后的消息轉(zhuǎn)換器處理方式

    這篇文章主要介紹了SpringMVC修改返回值類(lèi)型后的消息轉(zhuǎn)換器處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java 正確地從類(lèi)路徑中獲取資源

    Java 正確地從類(lèi)路徑中獲取資源

    Java 有能力從類(lèi)路徑中查找獲取資源,可將資源放在 CLASSPATH 里,也可打包到 Jar 中。本文將具體講述獲取資源的步驟,感興趣的朋友可以了解下
    2021-05-05
  • Java實(shí)現(xiàn)公眾號(hào)功能、關(guān)注及消息推送實(shí)例代碼

    Java實(shí)現(xiàn)公眾號(hào)功能、關(guān)注及消息推送實(shí)例代碼

    公眾號(hào)開(kāi)發(fā)近些年是一個(gè)比較熱門(mén)的方向,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)公眾號(hào)功能、關(guān)注及消息推送的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決

    mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決

    這篇文章主要介紹了mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • MyBatis的模糊查詢(xún)mapper.xml的寫(xiě)法講解

    MyBatis的模糊查詢(xún)mapper.xml的寫(xiě)法講解

    這篇文章主要介紹了MyBatis的模糊查詢(xún)mapper.xml的寫(xiě)法講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 簡(jiǎn)單談?wù)凧ava中String類(lèi)型的參數(shù)傳遞問(wèn)題

    簡(jiǎn)單談?wù)凧ava中String類(lèi)型的參數(shù)傳遞問(wèn)題

    這篇文章主要介紹了簡(jiǎn)單談?wù)凧ava中String類(lèi)型的參數(shù)傳遞問(wèn)題的相關(guān)資料,需要的朋友可以參考下
    2015-12-12

最新評(píng)論