发表于: 2018-01-21 21:44:02

1 662


今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin) 

完成了邮件接口的调用:

这个是拼接json的方法

public static String convert(List<Mail> dataList) {

JSONObject ret = new JSONObject();

  JSONArray to = new JSONArray();

  JSONArray names = new JSONArray();
  JSONArray urls = new JSONArray();

  for (Mail mail : dataList) {
to.put(mail.getAddress());
     names.put(mail.getName());
     urls.put(mail.getUrl());
  }

JSONObject sub = new JSONObject();
  sub.put("%name%", names);
  sub.put("%url%", urls);

  ret.put("to", to);
  ret.put("sub", sub);

  return ret.toString();
}

这是调用发送的方法:

    public static void send_template(Mail mail) throws ClientProtocolException, IOException {

final String url = "http://api.sendcloud.net/apiv2/mail/sendtemplate";

     final String apiUser = "tendk_test_hPmLQt";
     final String apiKey = "t46cAIxyTTu0Gyam";

     String subject = "...";

     List<Mail> dataList = new ArrayList<>();
     dataList.add(mail);

     final String xsmtpapi = convert(dataList);

     CloseableHttpClient httpClient = HttpClients.createDefault();
     HttpPost httpPost = new HttpPost(url);

     List<NameValuePair> params = new ArrayList<NameValuePair>();
     params.add(new BasicNameValuePair("apiUser", apiUser));
     params.add(new BasicNameValuePair("apiKey", apiKey));
     params.add(new BasicNameValuePair("xsmtpapi", xsmtpapi));
     params.add(new BasicNameValuePair("templateInvokeName", "test_template_active"));
     params.add(new BasicNameValuePair("from", "sendcloud@sendcloud.org"));
     params.add(new BasicNameValuePair("fromName", "SendCloud"));
//    params.add(new BasicNameValuePair("subject", subject));

     httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

     HttpResponse response = httpClient.execute(httpPost);

     if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 正常返回
        System.out.println(EntityUtils.toString(response.getEntity()));
     } else {
System.err.println("error");
     }
httpPost.releaseConnection();
  }

需要一个实体类来进行传参:

package com.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author Arike
* Create_at 2018/1/21 10:52
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Mail {
private String address;
   private String name;
   private String url;
}

.

这是测试邮件效果:

另外我对任务7的设计是,短信用于注册,邮件用于点击链接激活.头像为用户头像,所以我需要先搞一个用户页面.

所以我现在基本顺序是

①头像的上传

②邮件的验证

③手机短信验证码注册.


因为邮件到达上限了,所以先把上传和下载的接口试了一下,

//上传
@RequestMapping(value = "/file",method = RequestMethod.POST)
public String fileup(MultipartFile file) throws IOException {
InputStream is = file.getInputStream();
   OutputStream os = new FileOutputStream("/soft/"+file.getOriginalFilename());
   IOUtils.copy(is,os);
   is.close();
   os.close();
   return "user";
}

//下载
@RequestMapping(value = "/file",method = RequestMethod.GET)
public void download(HttpServletResponse response) throws IOException {
//拿到响应流
   ServletOutputStream os = response.getOutputStream();
   //读取需要被下载的文件
   FileInputStream is = new FileInputStream("/soft/1.jpg");
   //这个仅是为了能够使用中文名显示下载的文件.
   String  str = "好看的图.jpg";
   str = new String(str.getBytes("utf-8"),"ISO8859-1");
   response.setHeader("Content-Disposition","attachment;filename="+str);
   IOUtils.copy(is,os);
   is.close();
   os.close();
}


明天计划的事情:(一定要写非常细致的内容) 

打算一天撸完任务7
遇到的问题:(遇到什么困难,怎么解决的) 

null
收获:(通过今天的学习,学到了什么知识)

学习的第三方API需要仔细阅读.不然调用的时候容易出问题.


返回列表 返回列表
评论

    分享到