【Java】java上传文件到指定路径下

 /**
     * 没有指定文件夹时,自动创建,并上传到该文件下
     * @param stream
     */
    private static String uploadFileToTomcat(InputStream stream,String msgId,String companyId,String uid){
        String fileName = msgId + ".mp3";
        String filePath = "chatres/"+companyId + "/voice/" +uid+"/"+ fileName;
// 访问文件的url 类似于:https://sobotuat.aeonlife.com.cn/picture/chatres/fa80cd602dde489eb34207510aff607a/voice/eb4c03ed9caa4cdd840d99f4ab96ad3d/1111111.mp3
// 这都是从nginx里面配置的,"/picture 指向了/data/new/upload/"
        String realPath= ConfigUtil.getUrlPrefix() +filePath ;
        //上传到nginx的"/data/new/upload/"这个目录下
        String destination = "/data/new/upload/"+ filePath;
        JSONObject res = new JSONObject();
        res.put("code","00000");
        res.put("msg","转换语音成功");
        try {
            File uploadFile = new File(destination);  //上传到的目标地址
            FileUtils.copyInputStreamToFile(stream, uploadFile);
            res.put("url",realPath);
        } catch (IOException e) {
            res.put("code","00001");
            res.put("msg","转换语音失败");
            e.printStackTrace();
            System.err.println(e.getMessage());
        }
        return res.toJSONString();
    }

【Java】java上传文件到指定路径下_第1张图片 

nginx配置方式:

【Java】java上传文件到指定路径下_第2张图片

你可能感兴趣的:(------【Java】,●编程语言)