用户导入,物品导入

This commit is contained in:
2023-11-14 10:16:19 +08:00
parent dc1b11155a
commit a038772795
11 changed files with 563 additions and 116 deletions

View File

@@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@@ -21,6 +22,7 @@ import com.wx.application.nebula.graph.service.ImportGraphService;
import com.wx.application.nebula.graph.service.NebulaOperateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
@Slf4j
@RequestMapping("/gorse")
@@ -116,9 +118,14 @@ public class GorseController extends BaseController {
return success(gorseService.getRecommendByUser(gQo.getUserId(), gQo.getRecommendation(), gQo.getCategory(), gQo.getN()));
}
@PostMapping(value = "/get_similar_item")
public ResponseData getSimilarItem(@RequestBody GorseQ gQo) throws Exception {
return success(gorseService.getSimilarItem(gQo.getItemId(), gQo.getCategory(), gQo.getN()));
@PutMapping(value = "/bulk/{type}")
public ResponseData bulkUser(@RequestParam Map<String, String> data, @RequestParam("file") MultipartFile file, @PathVariable("type") String type) throws Exception {
return success(gorseService.bulkUserOrItem(data, file, type));
}
@GetMapping(value = "/get_bulk_user")
public ResponseData getBulkUser() throws Exception {
return success(gorseService.getBulkUser());
}

View File

@@ -1,17 +1,32 @@
package com.wx.application.gorse4j;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@Service
public class GorseService {
@@ -95,6 +110,45 @@ public class GorseService {
return Arrays.asList(this.request("GET", url, null, Item[].class));
}
/**
* 文件方式导入用户或者物品users\items
*/
public JSONObject bulkUserOrItem(Map<String, String> data, MultipartFile multipartFile, String type) throws IOException {
File file = new File("path/to/file");
FileUtils.writeByteArrayToFile(file, multipartFile.getBytes());
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
for (Map.Entry<String, String> entry : data.entrySet()) {
builder.addTextBody(entry.getKey(), entry.getValue());
}
builder.addBinaryBody("file", file, ContentType.APPLICATION_OCTET_STREAM, multipartFile.getOriginalFilename());
return this.sendPostFormData(this.endpoint + "/api/bulk/" + type, builder);
}
public File getBulkUser() throws IOException {
return this.request("GET", this.endpoint + "/api/bulk/users", null, File.class);
}
public JSONObject sendPostFormData(String url, MultipartEntityBuilder builder) throws IOException {
JSONObject resJson;
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
resJson = JSONObject.parseObject(EntityUtils.toString(responseEntity));
} catch (IOException e) {
throw e;
}
return resJson;
}
private <Request, Response> Response request(String method, String url, Request request, Class<Response> responseClass) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();