|
@@ -8,6 +8,15 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.qiniu.common.QiniuException;
|
|
|
+import com.qiniu.http.Response;
|
|
|
+import com.qiniu.storage.Configuration;
|
|
|
+import com.qiniu.storage.Region;
|
|
|
+import com.qiniu.storage.UploadManager;
|
|
|
+import com.qiniu.storage.model.DefaultPutRet;
|
|
|
+import com.qiniu.util.Auth;
|
|
|
+import com.szwl.constant.ConfigConsts;
|
|
|
import com.szwl.constant.ResponseCodesEnum;
|
|
|
import com.szwl.model.bean.*;
|
|
|
import com.szwl.model.bo.JsonUtils;
|
|
@@ -33,6 +42,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.servlet.ServletInputStream;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
@@ -1084,48 +1094,70 @@ public class IndexController {
|
|
|
@RequestMapping(value = "/sendLog.htm", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
|
|
|
@ResponseBody
|
|
|
public String uploadFile(HttpServletRequest request, HttpServletResponse response, String fileName) throws Exception {
|
|
|
- String fileFullPath = "/home/hboxs/log/" + fileName + ".txt";
|
|
|
-// String fileFullPath = "/root/uploadfile/apache-tomcat-8.5.42/" + fileName;
|
|
|
-
|
|
|
- InputStream input = null;
|
|
|
- FileOutputStream fos = null;
|
|
|
+// String fileFullPath = "/home/hboxs/log/" + fileName + ".txt";
|
|
|
+//// String fileFullPath = "/root/uploadfile/apache-tomcat-8.5.42/" + fileName;
|
|
|
+//
|
|
|
+// InputStream input = null;
|
|
|
+// FileOutputStream fos = null;
|
|
|
+// try {
|
|
|
+// input = request.getInputStream();
|
|
|
+// File file = new File("/home/hboxs/log/");
|
|
|
+// if (!file.exists()) {
|
|
|
+// file.mkdirs();
|
|
|
+// }
|
|
|
+// fos = new FileOutputStream(fileFullPath);
|
|
|
+// int size = 0;
|
|
|
+// byte[] buffer = new byte[1024];
|
|
|
+// while ((size = input.read(buffer, 0, 1024)) != -1) {
|
|
|
+// fos.write(buffer, 0, size);
|
|
|
+// }
|
|
|
+//
|
|
|
+// //响应信息 json字符串格式
|
|
|
+// Map<String, Object> responseMap = new HashMap<String, Object>();
|
|
|
+// responseMap.put("flag", true);
|
|
|
+//
|
|
|
+// //生成响应的json字符串
|
|
|
+// String jsonResponse = JSONObject.toJSONString(responseMap);
|
|
|
+// sendResponse(jsonResponse, response);
|
|
|
+// } catch (IOException e) {
|
|
|
+// //响应信息 json字符串格式
|
|
|
+// Map<String, Object> responseMap = new HashMap<String, Object>();
|
|
|
+// responseMap.put("flag", false);
|
|
|
+// responseMap.put("errorMsg", e.getMessage());
|
|
|
+// String jsonResponse = JSONObject.toJSONString(responseMap) ;
|
|
|
+// sendResponse(jsonResponse, response);
|
|
|
+// } finally {
|
|
|
+// if (input != null) {
|
|
|
+// input.close();
|
|
|
+// }
|
|
|
+// if (fos != null) {
|
|
|
+// fos.close();
|
|
|
+// }
|
|
|
+// }
|
|
|
+ //构造一个带指定 Region 对象的配置类
|
|
|
+ Configuration cfg = new Configuration(Region.region2());
|
|
|
+ cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
|
|
|
+ UploadManager uploadManager = new UploadManager(cfg);
|
|
|
+ //默认不指定key的情况下,以文件内容的hash值作为文件名
|
|
|
+ String key = "log/" + fileName + ".txt";
|
|
|
try {
|
|
|
- input = request.getInputStream();
|
|
|
- File file = new File("/home/hboxs/log/");
|
|
|
- if (!file.exists()) {
|
|
|
- file.mkdirs();
|
|
|
- }
|
|
|
- fos = new FileOutputStream(fileFullPath);
|
|
|
- int size = 0;
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
- while ((size = input.read(buffer, 0, 1024)) != -1) {
|
|
|
- fos.write(buffer, 0, size);
|
|
|
- }
|
|
|
-
|
|
|
- //响应信息 json字符串格式
|
|
|
- Map<String, Object> responseMap = new HashMap<String, Object>();
|
|
|
- responseMap.put("flag", true);
|
|
|
-
|
|
|
- //生成响应的json字符串
|
|
|
- String jsonResponse = JSONObject.toJSONString(responseMap);
|
|
|
- sendResponse(jsonResponse, response);
|
|
|
- } catch (IOException e) {
|
|
|
+ ServletInputStream input = request.getInputStream();
|
|
|
+ Auth auth = Auth.create(ConfigConsts.QINIU_CLOUD_ACCESS_KEY, ConfigConsts.QINIU_CLOUD_SECRET_KEY);
|
|
|
+ String upToken = auth.uploadToken(ConfigConsts.QINIU_CLOUD_BUCKET, key);
|
|
|
+ Response responseMap = uploadManager.put(input, key, upToken,null, null);
|
|
|
+ //解析上传成功的结果
|
|
|
+ DefaultPutRet putRet = new Gson().fromJson(responseMap.bodyString(), DefaultPutRet.class);
|
|
|
+ System.out.println(putRet.key);
|
|
|
+ System.out.println(putRet.hash);
|
|
|
+ } catch (Exception ex) {
|
|
|
//响应信息 json字符串格式
|
|
|
Map<String, Object> responseMap = new HashMap<String, Object>();
|
|
|
responseMap.put("flag", false);
|
|
|
- responseMap.put("errorMsg", e.getMessage());
|
|
|
+ responseMap.put("errorMsg", ex.getMessage());
|
|
|
String jsonResponse = JSONObject.toJSONString(responseMap);
|
|
|
sendResponse(jsonResponse, response);
|
|
|
- } finally {
|
|
|
- if (input != null) {
|
|
|
- input.close();
|
|
|
- }
|
|
|
- if (fos != null) {
|
|
|
- fos.close();
|
|
|
- }
|
|
|
}
|
|
|
-
|
|
|
- return null;
|
|
|
+ return "success";
|
|
|
}
|
|
|
|
|
|
/**
|