发表于: 2017-12-30 22:00:16
1 427
今天完成的事情:
完成了图片上传的Demo。
发送短信Demo。
发送邮件Demo。
节奏有点慢了,因为卡在了怎么获取前台传入图片的路径这里,始终获取的都不是正确的路径,只有文件名。这就很尴尬
下面还是贴点代码.....
@RequestMapping(value = "/upload",method = RequestMethod.POST)
public String upLoad(MultipartFile file, HttpServletRequest request, HttpServletResponse response, HttpSession session){
System.out.println("jinlaile");
String realPath=request.getPathInfo();
MultipartHttpServletRequest mr = (MultipartHttpServletRequest) file;
System.out.println(realPath);
//String fileName1 = file.getName();
try {
InputStream input = new FileInputStream();
System.out.println(input);
}catch (Exception e){
e.printStackTrace();
}
return "404";}
//这一块有问题,前台传入一个file对象,然后这边输入输出流需要的是String 这个String就是传入文件的路径,这个路径如何获取?
因为这边我直接获取一个文件的name是没有全部的路径的。这个地方还需要看看别人的东西。
public static void setPic(InputStream input){
String Objectkey = "photo13.jpg";
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
String accessKeyId = "LTAIJS9Ubv67ENfF";
String accessKeySecret = "lD1TZCBgQpg9wNxDaWoSU82ywTPBiA";
String bucketName = "test-for-java-jnshu";
ObjectMetadata objectMeta = new ObjectMetadata();
objectMeta.setContentType("jpg");
OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
try {
System.out.println("正在上传...");
//uploadFile(client, bucketName, Objectkey, uploadFilePath);
//InputStream input = new FileInputStream(uploadFilePath);
client.putObject(bucketName, Objectkey, input, objectMeta);
//System.out.println("正在下载...");
//downloadFile(client, BUCKET_NAME, Objectkey, downloadFilePath);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("完成");}
//这是上传和下载图片的东西。
public static void send_common() throws IOException {
new ClassPathXmlApplicationContext("classpath:spring.xml");
System.out.println(url+"**********");
Random r = new Random();
String code = String.valueOf(r.nextInt(999999));
String html = "你的验证码为"+code+",请在3分钟内输入。";
String rcpt_to ="1065348398@qq.com";
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient httpClient = HttpClients.createDefault();
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("apiUser", apiUser));
params.add(new BasicNameValuePair("apiKey", apiKey));
params.add(new BasicNameValuePair("to", rcpt_to));
params.add(new BasicNameValuePair("from", "sendcloud@sendcloud.org"));
params.add(new BasicNameValuePair("fromName", "SendCloud"));
params.add(new BasicNameValuePair("subject", subject));
params.add(new BasicNameValuePair("html", html));
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();
}上面的是发送邮件的demo
明天计划的事情:
明天继续搞事情哦。
遇到的问题:
主要就是那个获取值的问题。其他没什么。
收获:
嗯,学起来有点慢了。还需要专研。
评论