Download the file browser opens the file directly (Front)

The front section

// get string concatenation
function getFJInfo(name, url) {
	return "<tr><td style=''><a  href='javascript:void(0)' οnclick='getDownFile(\"" + url + "\",\"" + name + "\")'>" + name + "</a></td></tr>"
}
//document dowload
function getDownFile(url, name) {
	I have money = {
		"url": url
	};
	$.ajax({
		url: contextPath + '/product-label/file2Stream',
		type: 'GET',
		data: Base64.encode(JSON.encode(param)),
		dataType: "text",
		success: function(data) {
			downloadFile(name, data)
		}
	})
}
// download trigger event stream processing
function downloadFile(fileName, content) {
	var aLink = document.createElement('a');
	var blob = new Blob([content]);
	var evt = document.createEvent("MouseEvents");
	evt.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
	aLink.download = fileName;
	aLink.href = URL.createObjectURL(blob);
	aLink.dispatchEvent(evt)
}

 Background processing controller, reference http://blog.sina.com.cn/s/blog_87216a0001014sm7.html

/**
     * Return flow
     * 
     * @Param requestMap request parameters
     * @Param response returns an object
     */
    @RequestMapping(value = "/file2Stream", method = RequestMethod.GET)
    public void file2Stream(@Json Map<String, Object> requestMap, HttpServletResponse response) {
        try {
            String url = String.valueOf(requestMap.get("url"));
            // URL url =new URL(String.valueOf(requestMap.get("url")));
            InputStream iStream = getFileStream(url);
            OutputStream stream = response.getOutputStream();
            stream.write(StreamUtils.getBytes(iStream));
            stream.flush();
            stream.close();
        } catch (Exception e) {
            LOG.error("ProductSalesRecommendController.file2Stream  error | ({})", e);
        }
    }
 
    /**
     * HttpURLConnection get the network path of the file stream
     * 
     * @Param url link
     * @return InputStream
     * @throws IOException
     */
    private InputStream getFileStream(URL url) throws IOException {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod("GET");
        InputStream inStream = conn.getInputStream();
        return inStream;
    }
 
    /**
     * HttpClient get the network path of the file stream
     * 
     * @Param url link string
     * @return InputStream
     * @throws IllegalStateException
     * @throws IOException
     */
    private InputStream getFileStream(String url) throws IllegalStateException, IOException {
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout (httpParams, 5000); // set the connection timeout is 5 seconds
        HttpClient client = new DefaultHttpClient (httpParams); // generates a http client sends a request objects
        HttpResponse httpResponse = client.execute (new HttpGet (url)); // send a request and waits for response
        HttpEntity entity = httpResponse.getEntity (); // get content inside response
        InputStream inStream = entity.getContent();
        return inStream;
    }

  

//得到拼接字符串function getFJInfo(name, url) {return "<tr><td style=''><a  href='javascript:void(0)' οnclick='getDownFile(\"" + url + "\",\"" + name + "\")'>" + name + "</a></td></tr>"}//文件下载function getDownFile(url, name) {var param = {"url": url};$.ajax({url: contextPath + '/product-label/file2Stream',type: 'GET',data: Base64.encode(JSON.encode(param)),dataType: "text",success: function(data) {downloadFile(name, data)}})}//流处理触发下载事件function downloadFile(fileName, content) {var aLink = document.createElement('a');var blob = new Blob([content]);var evt = document.createEvent("MouseEvents");evt.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);aLink.download = fileName;aLink.href = URL.createObjectURL(blob);aLink.dispatchEvent(evt)}

Guess you like

Origin www.cnblogs.com/Andrew520/p/12382381.html