@RequestMapping(value = "getFileTest")
@ResponseBody
public String getFileTest(HttpServletRequest request) throws FileNotFoundException {
File file = new File("C:\\Users\\Administrator\\Desktop\\images");
File[] array = file.listFiles();
for(int i = 0;i<array.length;i++){
String name = array[i].getName();
String path = array[i].getPath();
String[] pathName = name.split(".");
name.substring(0, name.indexOf("."));
System.out.println(name.substring(0, name.indexOf(".")));
try {
LhOwnerspremisesInfo lhOwnerspremisesInfo = new LhOwnerspremisesInfo();
lhOwnerspremisesInfo.setItemCaption(name.substring(0, name.indexOf(".")));
List<LhOwnerspremisesInfo> list = lhOwnerspremisesInfoService.findList(lhOwnerspremisesInfo);
if(list!=null&&list.size()>0){
postFile11(list.get(0).getId(), path,name);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return renderResult(Global.TRUE, text("导入完成"));
}
public static String postFile11(String bizKey, String path,String name) throws ClientProtocolException, IOException {
String res = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = "http://192.168.1.123:8681/upload/permanent";
HttpPost httppost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000).build();
httppost.setConfig(requestConfig);
FileBody bin = new FileBody(new File(path));
StringBody comment = new StringBody("This is comment", ContentType.TEXT_PLAIN);
HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin).addPart("comment", comment).build();
httppost.setEntity(reqEntity);
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
res = EntityUtils.toString(entity);
JSONObject obj = JSON.parseObject(res);
saveFileUpload(bizKey, "lh_ownerspremises_image",res,name);
response.close();
} else {
res = EntityUtils.toString(entity, "UTF-8");
response.close();
throw new IllegalArgumentException(res);
}
return res;
}
public static void saveFileUpload(String bizKey, String bizType,String res,String old_name) {
String isNewFileUpload = "true";
if(Global.TRUE.equalsIgnoreCase(isNewFileUpload)){
if (!StringUtils.isBlank(bizKey) && !StringUtils.isBlank(bizType)) {
FileUploadExtendService fileUploadExtendService = SpringUtils.getBean(FileUploadExtendService.class);
JSONObject obj = JSON.parseObject(res);
String urls = obj.getString("file");
urls = '['+urls+']';
if(!StringUtils.isEmpty(urls)){
JSONArray json = JSONArray.parseArray(urls);
int size = json.size();
FileUploadExtend fileUploadExtend = null;
for(int i = 0 ; i<size ; i++){
JSONObject jSONObject = json.getJSONObject(i);
String url = jSONObject.getString("url");
String fileType = jSONObject.getString("file_type");
if(fileUploadExtend == null){
fileUploadExtend = new FileUploadExtend();
}
fileUploadExtend.setId(null);
fileUploadExtend.setBizKey(bizKey);
fileUploadExtend.setBizType(bizType);
fileUploadExtend.setUrl(url);
fileUploadExtend.setFileName(old_name);
fileUploadExtend.setFileType(fileType);
fileUploadExtendService.save(fileUploadExtend);
}
}
}
}
}
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>