Search in sources :

Example 6 with TypechoApiconfig

use of com.RuleApi.entity.TypechoApiconfig in project RuleApi by buxia97.

the class UploadController method uploadOssFile.

/**
 * 上传到oss
 */
@RequestMapping(value = "/ossUpload", method = RequestMethod.POST)
@ResponseBody
public String uploadOssFile(@RequestParam("file") MultipartFile file, @RequestParam(value = "token", required = false) String token) throws IOException {
    Integer uStatus = UStatus.getStatus(token, this.dataprefix, redisTemplate);
    if (uStatus == 0) {
        return Result.getResultJson(0, "用户未登录或Token验证失败", null);
    }
    TypechoApiconfig apiconfig = apiconfigService.selectByKey(1);
    // 获取上传文件MultipartFile
    // 返回上传到oss的路径
    OSS ossClient = new OSSClientBuilder().build(apiconfig.getAliyunEndpoint(), apiconfig.getAliyunAccessKeyId(), apiconfig.getAliyunAccessKeySecret());
    InputStream inputStream = null;
    // 检查是否是图片
    BufferedImage bi = ImageIO.read(file.getInputStream());
    if (bi == null) {
        return Result.getResultJson(0, "请上传图片文件", null);
    }
    try {
        // 获取文件流
        inputStream = file.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 生成时间,用于创建目录
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DATE);
    // 获取文件名称
    String filename = file.getOriginalFilename();
    // String eName = filename.substring(filename.lastIndexOf("."));
    // 应对图片剪裁后的无后缀图片
    String eName = "";
    try {
        eName = filename.substring(filename.lastIndexOf("."));
    } catch (Exception e) {
        filename = filename + ".png";
        eName = filename.substring(filename.lastIndexOf("."));
    }
    // 1.在文件名称中添加随机唯一的值
    String newFileName = UUID.randomUUID() + eName;
    // String uuid = UUID.randomUUID().toString().replaceAll("-","");
    filename = newFileName;
    String key = apiconfig.getAliyunFilePrefix() + "/" + year + "/" + month + "/" + day + "/" + filename;
    // 调用OSS方法实现上传
    ossClient.putObject(apiconfig.getAliyunAucketName(), key, inputStream);
    ossClient.shutdown();
    String url = apiconfig.getAliyunUrlPrefix() + key;
    Map<String, String> info = new HashMap<String, String>();
    info.put("url", url);
    return Result.getResultJson(1, "上传成功", info);
}
Also used : HashMap(java.util.HashMap) Calendar(java.util.Calendar) OSS(com.aliyun.oss.OSS) BufferedImage(java.awt.image.BufferedImage) TypechoApiconfig(com.RuleApi.entity.TypechoApiconfig) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with TypechoApiconfig

use of com.RuleApi.entity.TypechoApiconfig in project RuleApi by buxia97.

the class MailServiceImpl method send.

/**
 * 发送附件
 * @param subject 邮件主题
 * @param content 邮件内容
 * @param toEmails 接收的邮箱
 * @param ccPeoples 抄送人
 * @param bccPeoples 密送人,就是发送人和抄送人那里都不显示该收件人,但能收到邮件。
 * @param attachmentFilePaths 附件
 * @throws MessagingException
 */
@Override
public void send(String subject, String content, String[] toEmails, String[] ccPeoples, String[] bccPeoples, String[] attachmentFilePaths) throws MessagingException {
    // 附件处理需要进行二进制传输
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, defaultEncoding);
    TypechoApiconfig apiconfig = apiconfigService.selectByKey(1);
    String newEmailFromName = apiconfig.getWebinfoTitle();
    // log.info("from = " + from);emailFromName
    mimeMessageHelper.setFrom(newEmailFromName + "<" + username + ">");
    // 设置邮件的主题
    log.info("subject = " + subject);
    mimeMessageHelper.setSubject(subject);
    // 设置邮件的内容,区别是否是HTML邮件
    // 所有都以html格式发送
    mimeMessageHelper.setText(content, true);
    // 设置邮件的收件人
    log.info("toEmails = " + toEmails);
    mimeMessageHelper.setTo(toEmails);
    if (ccPeoples != null && ccPeoples.length > 0) {
        // 设置邮件的抄送人
        mimeMessageHelper.setCc(ccPeoples);
    }
    if (bccPeoples != null && bccPeoples.length > 0) {
        // 设置邮件的密送人
        mimeMessageHelper.setBcc(bccPeoples);
    }
    if (attachmentFilePaths != null && attachmentFilePaths.length > 0) {
        handleAttachments(mimeMessageHelper, subject, attachmentFilePaths);
    }
    mailSender.send(mimeMessage);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) TypechoApiconfig(com.RuleApi.entity.TypechoApiconfig)

Aggregations

TypechoApiconfig (com.RuleApi.entity.TypechoApiconfig)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 HashMap (java.util.HashMap)5 BufferedImage (java.awt.image.BufferedImage)4 Calendar (java.util.Calendar)3 MultipartFile (org.springframework.web.multipart.MultipartFile)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ApplicationHome (org.springframework.boot.system.ApplicationHome)2 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)1 COSCredentials (com.qcloud.cos.auth.COSCredentials)1 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)1 PutObjectResult (com.qcloud.cos.model.PutObjectResult)1 Region (com.qcloud.cos.region.Region)1 Map (java.util.Map)1 MimeMessage (javax.mail.internet.MimeMessage)1