dfs文件服務器訪問權限,fastDFS 文件服務器訪問

鑒權 token 獲取

token 由文件服務器管理員分配

接口定義

上傳文件

請求 URL:

請求方式:

GET/POST

參數形式:

form-data

參數:

參數名位置類型說明是否必填

access_tokenheaderString用戶 token是

fileurlMultipartFile文件是

返回:

參數名必選類型說明

msg是String提示

code是Integer錯誤代碼

data是String數據

data 域內容

參數名必選類型說明

filePath是String文件的存儲位置

fileName是Integer文件的原始名稱

fileType是String文件類型

httpUrl是String文件的訪問地址(未開啟防盜鏈時可用)

返回示例

{

"msg": "操作成功",

"code": 200,

"data": {

"filePath": "group1/M00/00/0D/wKjcAl-SNgmAVJK7AACEAGBrbtI245.jpg",

"fileName": "timg.jpg",

"fileType": "jpg",

"httpUrl": "http://192.168.220.2:80/group1/M00/00/0D/wKjcAl-SNgmAVJK7AACEAGBrbtI245.jpg"

}

}

備注

獲取防盜鏈地址

請求 URL:

請求方式:

GET/POST

參數形式:

form-data

參數:

參數名位置類型說明是否必填

access_tokenheaderString用戶 token是

filePathurlString文件路徑是

返回:

參數名必選類型說明

msg是String提示

code是Integer錯誤代碼

data是String文件的防盜鏈訪問路徑

返回示例

{

"msg": "操作成功",

"code": 200,

"data": "http://192.168.220.2:80/group1/M00/00/0D/wKjcAl-SNgmAVJK7AACEAGBrbtI245.jpg?token=639e5738e7c457eb2f061fc0d71a3165&ts=1603417665"

}

備注

刪除文件

請求 URL:

請求方式:

GET/POST

參數形式:

form-data

參數:

參數名位置類型說明是否必填

access_tokenheaderString用戶 token是

filePathurlString文件路徑是

返回:

參數名必選類型說明

msg是String提示

code是Integer錯誤代碼

data是Integer0 為正常響應,非 0 為異常響應,文件不存在等

返回示例

{

"msg": "操作成功",

"code": 200,

"data": 0

}

備注

java 調用工具類

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.HashMap;

import java.util.Map;

public class HttpClientUtil {

private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

/**

* 路徑分隔符

*/

public static final String SEPARATOR = "/";

/**

* Point

*/

public static final String POINT = ".";

/**

* ContentType

*/

public static final Map EXT_MAPS = new HashMap<>();

static {

initExt();

}

public static void initExt() {

// image

EXT_MAPS.put("png", "image/png");

EXT_MAPS.put("gif", "image/gif");

EXT_MAPS.put("bmp", "image/bmp");

EXT_MAPS.put("ico", "image/x-ico");

EXT_MAPS.put("jpeg", "image/jpeg");

EXT_MAPS.put("jpg", "image/jpeg");

// 壓縮文件

EXT_MAPS.put("zip", "application/zip");

EXT_MAPS.put("rar", "application/x-rar");

// doc

EXT_MAPS.put("pdf", "application/pdf");

EXT_MAPS.put("ppt", "application/vnd.ms-powerpoint");

EXT_MAPS.put("xls", "application/vnd.ms-excel");

EXT_MAPS.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

EXT_MAPS.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");

EXT_MAPS.put("doc", "application/msword");

EXT_MAPS.put("doc", "application/wps-office.doc");

EXT_MAPS.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

EXT_MAPS.put("txt", "text/plain");

// 音頻

EXT_MAPS.put("mp4", "video/mp4");

EXT_MAPS.put("flv", "video/x-flv");

}

/**

* @Title: postRestData

* @TitleExplain: post請求接口

* @Description: post請求接口

* @param urlStr 請求url

* @return String post響應

*/

public static String postDataLikeFormData(String urlStr,MultipartFile file,String fastdfsAccessToken) {

String result = "";

// 換行符

final String newLine = "\r\n";

final String boundaryPrefix = "--";

// 定義數據分隔線

String BOUNDARY = "========7d4a6d158c9";

// 服務器的域名

URL url =null;

HttpURLConnection conn=null;

DataInputStream in=null;

try {

url = new URL(urlStr);

conn = (HttpURLConnection) url.openConnection();

// 設置為POST情

conn.setRequestMethod("POST");

// 發送POST請求必須設置如下兩行

conn.setDoOutput(true);

conn.setDoInput(true);

conn.setUseCaches(false);

// 設置請求頭參數

conn.setRequestProperty("connection", "Keep-Alive");

conn.setRequestProperty("Charsert", "UTF-8");

conn.setRequestProperty("access_token",fastdfsAccessToken);

conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

OutputStream out = new DataOutputStream(conn.getOutputStream());

/**

* 循環輸出

*

* */

StringBuilder sb = new StringBuilder();

sb.append(boundaryPrefix);

sb.append(BOUNDARY);

sb.append(newLine);

sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getOriginalFilename()

+ "\"" + newLine);

String contentType=getContentTypeByFileName(file.getOriginalFilename());

sb.append("Content-Type:"+contentType);

sb.append(newLine);

sb.append(newLine);

out.write(sb.toString().getBytes());

byte[] bufferOut = new byte[1024];

int bytes = 0;

//如果文件為空則不上傳

if (file.isEmpty() || file.getSize() <= 0) {

logger.debug("文件為空或大小為0沒有上傳fastdfs:" + file.getOriginalFilename());

return null;

}

in = new DataInputStream(file.getInputStream());

while ((bytes = in.read(bufferOut)) != -1) {

//bufferOut轉化為String之后會損失部分數據,所以之后的操作直接輸出,不轉化為string

// sb.append(new String(bufferOut));

out.write(bufferOut, 0, bytes);

}

sb.append(newLine + boundaryPrefix + BOUNDARY);

sb.append(boundaryPrefix + newLine);

byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine).getBytes();

out.write(end_data);

logger.debug("請求數據為: "+sb.toString());

out.flush();

out.close();

StringBuffer sbResult = new StringBuffer();

// 定義BufferedReader輸入流來讀取URL的響應

BufferedReader reader = new BufferedReader(new InputStreamReader(

conn.getInputStream()));

String line = null;

while ((line = reader.readLine()) != null) {

//System.out.println(line);

sbResult.append(line+"\r\n");

}

reader.close();

result = sbResult.toString();

} catch (Exception e) {

logger.error("發送POST請求出現異常!" + e);

}finally {

if (conn != null) {

conn.disconnect();

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

logger.debug("post "+urlStr +",result="+result);

return result;

}

/**

* 根據文件名獲取contentType

* */

public static String getContentTypeByFileName(String fileName){

return EXT_MAPS.get(getFilenameSuffix(fileName));

}

/**

* 獲取文件名稱的后綴

*

* @param filename 文件名 或 文件路徑

* @return 文件后綴

*/

public static String getFilenameSuffix(String filename) {

String suffix = null;

String originalFilename = filename;

if (StringUtils.isNotBlank(filename)) {

if (filename.contains(SEPARATOR)) {

filename = filename.substring(filename.lastIndexOf(SEPARATOR) + 1);

}

if (filename.contains(POINT)) {

suffix = filename.substring(filename.lastIndexOf(POINT) + 1);

} else {

if (logger.isErrorEnabled()) {

logger.error("filename error without suffix : {}", originalFilename);

}

}

}

return suffix;

}

/**

* 從指定系統獲取寫卡數據

*

* */

public static String downloadFile(String urlStr,String filePath,String fastdfsAccessToken){

String result = null;

try{

HttpClient client = new DefaultHttpClient();

StringBuffer sb=new StringBuffer(urlStr);

StringBuffer params=new StringBuffer("");

params.append("filePath=");

params.append(filePath);

HttpPost post = new HttpPost(sb.toString());

//設置header參數

post.addHeader("access_token",fastdfsAccessToken);

//設置其它參數

StringEntity stringEntity = new StringEntity(params.toString());//param參數,可以為"key1=value1&key2=value2"的一串字符串

stringEntity.setContentType("application/x-www-form-urlencoded");

post.setEntity(stringEntity);

HttpResponse resp = client.execute(post);

BufferedReader brBufferedReader = new BufferedReader(

new InputStreamReader(resp.getEntity().getContent(), "utf-8"));

StringBuffer resultSb = new StringBuffer();

String line = "";

while ((line = brBufferedReader.readLine()) != null) {

resultSb.append(line);

}

brBufferedReader.close();

result = resultSb.toString();

}catch(Exception e){

logger.error("系統異常:",e);

}

return result;

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540769.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540769.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540769.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

CCFL的完整形式是什么?

CCFL&#xff1a;冷陰極熒光燈 (CCFL: Cold Cathode Fluorescent Lamp) CCFL is an abbreviation of a "Cold Cathode Fluorescent Lamp". CCFL是“冷陰極熒光燈”的縮寫。 It is a lighting system lamp that contains cathode that discharges electrons and it …

ffmpeg 純靜態編譯,以及添加自定義庫流程摘要

需求&#xff1a; 1. 純靜態編譯ffmpeg ,即ldd ./ffmpeg 的結果是&#xff1a;not a dynamic executable2. 修改ffmpeg 項目&#xff0c;添加自定義功能庫3. 自定義庫由c實現&#xff0c;要求能被純c的ffmpeg項目調用4. 自定義庫必須使用g 的一些高級特性編譯&#xff0c;要求…

vue ani_ANI的完整形式是什么?

vue aniANI&#xff1a;自動號碼識別 (ANI: Automatic Number Identification) ANI is an abbreviation of "Automatic number identification". ANI是“自動號碼識別”的縮寫 。 It is an attribute of a network of telecommunications for involuntarily finding…

realme系統服務器代碼,解鎖BL之后,Realme正式開放源代碼

集微網8月30日消息(文/數碼控)&#xff0c;此前Realme已經開放了解鎖BootLoader(簡稱BL)&#xff0c;現在官方更進一步&#xff0c;直接將Realme X、Realme X青春版的源代碼開放了。可能有的人不知道解鎖BL與開放源代碼是什么意思&#xff0c;我們在此來說明一下&#xff1a;Bo…

Codeforces 757B - Bash's Big Day(分解因子+hashing)

757B - Bashs Big Day 思路&#xff1a;篩法。將所有因子個數求出&#xff0c;答案就是最大的因子個數&#xff0c;注意全為1的特殊情況。 代碼&#xff1a; #include<bits/stdc.h> using namespace std; #define ll long long #define pb push_back const int N1e55; in…

JavaScript中的const

const (const) Like other programming languages, JavaScript also provide the feature to create constants, we can make any identifier as constant by using the "const". 與其他編程語言一樣&#xff0c;JavaScript也提供了創建常量的功能&#xff0c;我們可…

無法從ftp服務器上復制文件格式,ftp服務器上復制不了文件格式

ftp服務器上復制不了文件格式 內容精選換一換本版本提供dump_data_conversion.pyc腳本&#xff0c;實現dump數據文件與numpy文件格式互轉功能&#xff0c;具體命令行格式如下&#xff1a;-type&#xff1a;數據類型&#xff0c;必選參數 。參數值選項&#xff1a;quant&#xf…

華大基因茅矛:云計算讓精準醫療走進生活

2016年是“十三五”的開局之年&#xff0c;也是中國醫療衛生行業的關鍵一年。現在看來&#xff0c;也會是醫療行業和以大數據為代表的信息技術相互融合發展之年。今年4月&#xff0c;國務院辦公廳印發《深化醫藥衛生體制改革2016年重點工作任務》&#xff0c;其中不僅談到了要加…

Python Pandas –操作

Pandas support very useful operations which are illustrated below, 熊貓支持非常有用的操作&#xff0c;如下所示&#xff0c; Consider the below dataFrame, 考慮下面的dataFrame&#xff0c; import numpy as npimport pandas as pddf pd.DataFrame({col1: [1, 2, 3,…

有道詞典總顯示無法連接服務器,有道詞典無法聯網提示網絡已斷開該怎么辦

人們使用電腦時候最不想看到的事情之一就是上不了網了&#xff0c;無論是工作還是玩游戲時候都很不爽。電腦能正常上網&#xff0c;但是有道詞典始終無法聯網。這是怎么回事呢?下面一起看看!方法步驟1、我是win8的系統。有道詞典無法聯網后&#xff0c;我在網上查了一下方法&a…

ajax+lazyload時lazyload失效問題及解決

最近寫公司的項目的時候遇到一個關于圖片加載的問題&#xff0c;所做的頁面是一個商城的商品列表頁&#xff0c;其中需要顯示商品圖片&#xff0c;名稱等信息&#xff0c;因為商品列表可能會很長&#xff0c;所以其中圖片需要滑到可以顯示的區域再進行加載。 首先我的圖片加載插…

手游pubg mobile服務器正在維護,PUBG Mobile Download Failed怎么解決

《PUBG Mobile》國際服出現下載失敗的情況&#xff0c;你將會收到“Download Failed”提示&#xff0c;你就需要按照下述的方法去解決該問題。注意&#xff1a;如果下載不了 請復制瀏覽器上的鏈接 https:/http://pic.81857.netownloads.gradle.orghttp://pic.81857.netistribut…

Python自動化運維之常用模塊—logging

在現實生活中&#xff0c;記錄日志非常重要。銀行轉賬時會有轉賬記錄&#xff1b;如果有出現什么問題&#xff0c;人們可以通過日志數據來搞清楚到底發生了什么。 對于系統開發、調試以及運行&#xff0c;記錄日志都是同樣的重要。如果沒有日志記錄&#xff0c;程序崩潰時你…

Sys.WORD_SIZE Julia中的常量

Julia| Sys.WORD_SIZE常數 (Julia | Sys.WORD_SIZE Constant) Sys.WORD_SIZE is a constant of the Int64 type in Julia programming language, it is used to get the standard word size of the current system. Sys.WORD_SIZE是Julia編程語言中Int64類型的常量&#xff0c;…

ftp服務器如何配置多個文件夾,ftp服務器如何配置多個文件夾

ftp服務器如何配置多個文件夾 內容精選換一換Model File:模型文件。單擊右側的文件夾圖標&#xff0c;在后臺服務器sample所在路徑(工程目錄/run/out/test_data/model)選擇需要轉化的模型對應的*.prototxt文件&#xff0c;并上傳。Weight File:權重文件。請自行從https://obs-m…

scala 方法調用_Scala中的方法調用

scala 方法調用Scala方法調用 (Scala Method Invocation) Method invocation is the legal and correct technique to call a method in Scala programming language. The methods of a class are usually accessed by using two methods. 方法調用是用Scala編程語言調用方法的…

docker lnmp php

使用 Docker 構建 LNMP 環境https://segmentfault.com/a/1190000008833012Docker 快速上手指南https://segmentfault.com/a/1190000008822648#articleHeader44

根據分類id找出父類id

2019獨角獸企業重金招聘Python工程師標準>>> 數組格式要求 id > pid $columns [ 1 > 0, 10 > 1, 200 > 10 ]; public function getP($columns,$pid) { 模擬 $pid 200; $arr $columns; while($arr[$pid]) { …

不穩定學習器適合做基分類器_分類穩定性

不穩定學習器適合做基分類器什么是分類&#xff1f; (What is sorting?) It means to arrange data elements in increasing or decreasing order. There are many algorithms to do so like mergesort, quicksort, counting sort etc but there are pros and cons of each al…

JavaScript基礎學習--05自定義屬性、索引值

Demos&#xff1a; https://github.com/jiangheyan/JavaScriptBase 一、自定義屬性1、讀寫操作<input abc"123" type"button" value"按鈕" />//讀寫&#xff1a; var aBtn document.getElementsByTagName(input); aBtn[0].abc 456; 2、…