小程序上传音频失败_小程序采集录音并上传到后台

package com.azor.controller;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;

import org.apache.commons.lang.StringUtils;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.core.env.Environment;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.azor.utils.HttpAPIService;

import ch.qos.logback.classic.Logger;

import net.sf.json.JSONObject;

@RestController

@RequestMapping("/base_voice")

public class BaseController {

private static final Logger logger = (Logger) LoggerFactory.getLogger(BaseController.class);

private static String lineSeparator = System.getProperty("line.separator");

@Autowired

protected Environment env;

@Autowired

protected HttpAPIService httpAPIService;

/** 上传文件保存路径 */

private final String FILE_SAVE_PATH = "D:/photo/jac_hr_miniprogram_file/";

/** 主业务数据Map */

protected Map dataMap = new HashMap<>();

/** HTTP POST 请求Map */

protected Map postMap = new HashMap<>();

@RequestMapping("/file_upload")

public void saveFile(HttpServletRequest request, String url) throws Exception {

logger.info("文件上传开始" + lineSeparator);

// 1.获取从前台传过来得图片

MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;

MultipartFile multipartFile = req.getFile("file");

// 2.获得文件扩展名

String extOfFile = getExtOfFile(multipartFile);

// 3.保存到本地

BufferedOutputStream bos = null;

String filename = null;

try {

File dir = new File(file_save_path);

if (!dir.exists()) {// 判断文件目录是否存在

dir.mkdirs();

}

filename = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + (int) (Math.random() * 1000) + "."

+ extOfFile;

bos = new BufferedOutputStream(new FileOutputStream(file_save_path + filename));

bos.write(multipartFile.getBytes());

} catch (Exception e) {

e.printStackTrace();

} finally {

if (bos != null) {

try {

bos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public String getExtOfFile(MultipartFile multipartFile) {

// 获取文件的 名称.扩展名

String oldName = multipartFile.getOriginalFilename();

String extensionName = "";

// 获取原来的扩展名

if ((oldName != null) && (oldName.length() > 0)) {

int dot = oldName.lastIndexOf('.');

if ((dot > -1) && (dot < (oldName.length() - 1))) {

extensionName = oldName.substring(dot+1);

}

}

return extensionName;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值