图片上传服务器 获取返回上传的地址

 

public static Map<String,Object> UpIOFile(byte[] bbyte,String type){
			String uploadUrl = "上传到的地址";
			String end = "\r\n";
			String twoHyphens = "--";
			String boundary = "---------------------------823928434";
			try {
				URL url = new URL(uploadUrl);
				HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
				httpURLConnection.setDoInput(true);
				httpURLConnection.setDoOutput(true);
				httpURLConnection.setUseCaches(false);
				httpURLConnection.setRequestMethod("POST");
				httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
				httpURLConnection.setRequestProperty("Charset", "UTF-8");
				httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
				DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
				dos.writeBytes(twoHyphens + boundary + end);
				dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"test."+type+"\"" + end);
				dos.writeBytes(end);
				dos.write(bbyte);
				dos.writeBytes(end);
				dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
				dos.flush();
				// 读取服务器返回结果
				InputStream is = httpURLConnection.getInputStream();
				InputStreamReader isr = new InputStreamReader(is, "utf-8");
				BufferedReader br = new BufferedReader(isr);
				String result = br.readLine();

				is.close();
				Map<String,Object> resultMap = JSON.parseObject(result,Map.class);
				return resultMap;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

//图片转为file格式
File file = FileUtil.file(spreadPicPath);
		FileInputStream fileInputStream = new FileInputStream(file);
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = fileInputStream.read(buffer)) != -1) {
			//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
			outStream.write(buffer, 0, len);
		}
		Map<String, Object> map = MutiPartFileUtil.UpIOFile(outStream.toByteArray(), "jpg");
        map.put("url",map.get("url"));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ThinkPHP 是一个开源的 PHP 框架,它简化了 PHP 开发者的开发流程。当你需要在 ThinkPHP 中处理用户上传 base64 编码的图片时,你可以按照以下步骤操作: 1. 首先,接收前端提交的 base64 图片数据。这通常发生在控制器(Controller)的方法中,例如 `uploadAction`: ```php public function uploadAction(Request $request) { $base64Data = $request->post('image_base64'); // 其他验证和处理 } ``` 2. 解码 base64 数据为二进制流: ```php $data = base64_decode($base64Data); ``` 3. 创建临时文件并保存解码后的图片: ```php $tempFile = tempnam(THINK_ROOT . 'public/upload', 'img_'); // 假设你有一个公共文件夹 upload file_put_contents($tempFile, $data); ``` 4. 使用 ThinkPHP 的上传功能(如 `upload` 函数)将图片移动到服务器上指定的目录,并获取新的文件名或路径。这里假设你使用的是官方的上传配置: ```php $upload = new \Think\Upload(); $upload->isCheck(false); // 关闭验证,因为我们已经有了 base64 数据 $upload->savePath = '/path/to/your/upload/directory'; // 设置保存路径 $info = $upload->upload($tempFile); ``` 5. 如果上传成功,`$info` 将包含文件信息,包括新路径。你可以像这样获取路径: ```php $filePath = $info['savepath'] . '/' . $info['name']; ``` 6. 清理临时文件(如果需要): ```php unlink($tempFile); ``` 7. 返回上传路径给前端: ```php $response['url'] = $filePath; echo json_encode($response); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值