多類型文件預覽和下載 以及處理txt亂碼 兼容手機端

文件預覽和下載

最近公司裏面有個手機微信端的文件預覽跟下載功能,文件類型包括

  • txt.pdf.doc.docx.xls.xlsx.rar.zip

首先。是下載,這個是在web上開發的,首先我用的是window.open(),然後。。。到測試的時候就炸了。原因是這東西在電腦顯示的是下載,但在手機上面的是直接預覽,而且txt文件還是亂碼

於是乎。。。我的預覽功能一不小心用下載的方式實現了。。。
下載,我用的是後臺獲取linux服務器文件,以流的形式生成新的文件下載
,前臺只需傳入一個參數文件的絕對路徑,fileName如:/upload/file/org00000000/20190428/38a9921cd62a4273bfc5b055de456070.docx (這裏的是linux 下的文件路徑)
代碼如下

public void downLoadFile() {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpServletResponse rep = ServletActionContext.getResponse();
		BufferedOutputStream bufout=null;
		InputStream in = null;
		BufferedInputStream bufin =null;
		OutputStream out = null;
		ServletOutputStream os =null;
		URL url = null;
		HttpURLConnection urlconn = null;
		try {
			String[] str=fileName.split("/");
			String newFileName=str[str.length-1];//截取源文件的文件名作爲文件的文件名
			rep.setContentType("multipart/form-data");
			rep.setHeader("Content-disposition","attachment;filename="+ URLEncoder.encode(newFileName,"utf-8"));
			//創建url連接
			url = new URL("服務器域名(不方便展示)"+fileName);
			urlconn = (HttpURLConnection) url.openConnection();
			urlconn.connect();
//			File file = new File(fileName);

			bufin = new BufferedInputStream(urlconn.getInputStream());
			File newFile = new File("域名//"+fileName);
//			out = new FileOutputStream(newFile);
			os=rep.getOutputStream();
			bufout= new BufferedOutputStream(os);
			byte[] bytes = new byte[2048];
			int length=0;

			while((length=bufin.read(bytes))!=-1){
				bufout.write(bytes,0,length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				bufin.close();
				bufout.close();
				os.close();
				urlconn.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

		doJsonOut();
	}

實現下載的docx文件下載如下在這裏插入圖片描述
這種方式的好處就是下載的txt,xls都不會亂碼,而且兼容多種文件格式,而且。。。copy基本就可以直接使用,不需要用到任何插件(網上找了兩天,下載的方法花式百樣,總結就是一個字,坑)

預覽使用window.open(), txt會出現亂碼問題

這個,其實我問了一些大神,他的回答是新建一個jsp文件返回字符串不就行了嗎
我咧個去。。。我百度找了那麼久的問題結果就一句話被解決的。。然後,我把上面的方法修改了一下,把流轉成字符串,然後,新建個viewfile.jsp 。。。嘿嘿嘿
由於項目使用了struts ,所以返回的類型是用request.setAttribute的方式,直接在新的頁面獲取就行了
代碼基本差不多,如下:

public String viewFile(){
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpServletResponse rep = ServletActionContext.getResponse();
		BufferedOutputStream bufout=null;
		InputStream in = null;
		BufferedInputStream bufin =null;
		OutputStream out = null;
		ServletOutputStream os =null;
		URL url = null;
		HttpURLConnection urlconn = null;
		try {
			String[] str=fileName.split("/");
			String newFileName=str[str.length-1];//截取源文件的文件名作爲文件的文件名
			rep.setCharacterEncoding("UTF-8");
			rep.setContentType("html/text");
			//創建url連接
			url = new URL("域名/"+fileName);
			urlconn = (HttpURLConnection) url.openConnection();
			urlconn.connect();
//			File file = new File(fileName);
			bufin = new BufferedInputStream(urlconn.getInputStream());
			File newFile = new File("域名/"+fileName);
//			out = new FileOutputStream(newFile);
//			os=rep.getOutputStream();
//			bufout= new BufferedOutputStream(os);
			byte[] bytes = new byte[2048];
			int length=0;
			String content="";
			while((length=bufin.read(bytes))!=-1){
//				bufout.write(bytes,0,length);
				content+=new String(bytes,"utf-8");
			}
			req.setAttribute("content",content);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				bufin.close();
//				bufout.close();
//				os.close();
				urlconn.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return "viewfile";
	}

下面是方法入口,大同小異而已
在這裏插入圖片描述
viewfile.jsp 頁面 簡單,粗暴
在這裏插入圖片描述
功能顯示
在這裏插入圖片描述

博主還遇到了一個跟噁心的問題。。。js不兼容。。

所有測試人員測試完都沒問題了。。然後。。項目經理一測,臥槽,又炸了。。。

按鈕那些都顯示不了
原因只是。。。那土豪用的是蘋果,我咧個去,,,幾個大佬的一個個調試,都發現不了問題,我走了一下流程,發現一到判斷時間類型下面的js都失效了。

百度一看,好嘛,這蘋果貨不兼容 yyyy-MM-dd 的時間格式,除此之外還不兼容綁定的按鈕點擊事件,略作修改,寫成yyyy/MM/dd,換一下綁定,完美渡劫成功,

臥槽,肝了一個月的項目,終於弄完了,博主今晚吃飯要加菜,至少也要加個雞腿

下邊加個判斷時間類型的js的吧
時間類型 yyyy/MM/dd, 具體yyyy-MM-dd能不能用沒測試過,有測試成功的小夥伴可以留個言提醒一下別人。。。
功能,比較傳入來的時間(String 類型)與當前時間的大小,(精確到分,需要更精確的只要你認真看一下就知道怎麼修改了)

 function betterNow(endTime){
        var mydate = new Date();
        var inputDate=new Date(endTime);
        if(inputDate>mydate){
            return true
        }else
            return false;
    }

對了,我還有一句話想說

iphone就是個辣雞~~~~~~~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章