欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

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

来源: javaer 分享于  点击 9910 次 点评:266

【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();
    }

 

nginx配置方式:

相关文章

    暂无相关文章
相关栏目:

用户点评