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>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值