首页 / JAVA / java 读取本地图片上传
java 读取本地图片上传
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java 读取本地图片上传,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3818字,纯文字阅读大概需要6分钟。
内容图文
![java 读取本地图片上传](/upload/InfoBanner/zyjiaocheng/593/bd251f83808b4d6797111db1f02bee5f.jpg)
@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){
// list.get(0).getId();
postFile11(list.get(0).getId(), path,name);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
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)); // 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);
// httppost.setEntity(getMutipartEntry(param,file));
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// res = EntityUtils.toString(entity, "UTF-8");
res = EntityUtils.toString(entity);
JSONObject obj = JSON.parseObject(res);
// FileUploadExUtils.saveFileUpload("test", "test_image");
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 old_name = jSONObject.getString("old_name");
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>
内容总结
以上是互联网集市为您收集整理的java 读取本地图片上传全部内容,希望文章能够帮你解决java 读取本地图片上传所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。