java 读取本地图片上传
2021/4/12 12:25:11
本文主要是介绍java 读取本地图片上传,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
@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 读取本地图片上传的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-28一步到位:购买适合 SEO 的域名全攻略
- 2024-12-27OpenFeign服务间调用学习入门
- 2024-12-27OpenFeign服务间调用学习入门
- 2024-12-27OpenFeign学习入门:轻松掌握微服务通信
- 2024-12-27OpenFeign学习入门:轻松掌握微服务间的HTTP请求
- 2024-12-27JDK17新特性学习入门:简洁教程带你轻松上手
- 2024-12-27JMeter传递token学习入门教程
- 2024-12-27JMeter压测学习入门指南
- 2024-12-27JWT单点登录学习入门指南
- 2024-12-27JWT单点登录原理学习入门