springmvc上传图片,发送邮件


package hcxAction;

 

import hcxMode.Advertises;

import hcxMode.Areas;

import hcxMode.Saveresume;

import hcxService.AdvertisesService;

import hcxService.SaveresumeService;

 

import java.io.File;

import java.io.IOException;

import java.util.List;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

 

import org.apache.commons.io.FileUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.multipart.MultipartFile;

 

import com.util.mail.MailSenderInfo;

import com.util.mail.SimpleMailSender;

 

/**

* @author cunli

*招聘

*/

@Controller

@RequestMapping("/advertisea")

public class AdvertiseAction {

@Autowired

private AdvertisesService advertisesService;

@Autowired

private SaveresumeService saveresumeService;

UsersAction usersAction=new UsersAction();

 

/**

* @param model

* @param request

* @return 发布成功

*/

@RequestMapping("/zhaopinissuesuccess")

public String zhaopinissuesuccess( Advertises advertises, Model model,HttpSession session) throws IOException {

 

 

advertises.setUserId(usersAction.uid(session));

 

// 根据userid查询最大的id

int maxid = advertisesService.advertiseidService(advertises);

advertises.setId(maxid);

model.addAttribute("zwid", maxid);

int success = advertisesService.updatexiangxiService(advertises);

 

if (success == 0) {

model.addAttribute("errormsg", "信息发布失败,请重试");

return "/operate/particulars";

}

 

return "/operate/issuesuccess";

}

 

 

/**

* @param model

* @param request

* @param position

* @return 填写详情

*/

@RequestMapping("/particulars")

public String particulars(Model model,HttpSession session,Advertises advertises) {

advertises.setUserId(usersAction.uid(session));

int ok = advertisesService.addmsgService(advertises);

if (ok == 0) {// faid

model.addAttribute("faidmsg", "添加数据失败 ## 你可以重试一次");

return "/navigations/issuepostion";

}

return "/operate/particulars";

}

 

 

/**

* 预览图片

*

* @param files

* @param model

* @param request

* @param response

* @return

* @throws IOException

*/

@RequestMapping("/instantuploadview")

public String instanupload(

@RequestParam(required = false) MultipartFile[] files, Model model,

HttpServletRequest request)

throws IOException {

String apurl = "";

for (MultipartFile file : files) {

if (file.isEmpty()) {

return "";

} else {

 

// 1.===============替换文件名 并copy上传图片(原图)===============

String fileName = file.getOriginalFilename();

 

String realPath = request.getSession().getServletContext()

.getRealPath("\\");// 项目磁盘路径

// 预览的文件名 userName.jpg xxx.jpg ---nametime.jpg(利用时间戳解决浏览器图片缓存问题)

String randomm = String.valueOf(Math.random() * 10);

String viewplc = fileName.replace(

fileName.substring(0, fileName.lastIndexOf(".")),

randomm + System.currentTimeMillis());

 

// 保存 的文件名uuid.jpg

String saveplc = fileName.replace(

fileName.substring(0, fileName.lastIndexOf(".")),

randomm + System.currentTimeMillis());

 

// 项目磁盘绝对路径---->图片读取路径

String viewPath = realPath + "images" + "\\companyphoto\\"

+ viewplc;

 

// 项目磁盘绝对路径

String savePath = realPath + "images" + "\\companyphoto\\"

+ saveplc;

FileUtils.copyInputStreamToFile(file.getInputStream(),

new File(viewPath));

System.out.println("**************图片磁盘绝对路径********** "

+ savePath);

 

// 预览路径

apurl = "/images/companyphoto/" + viewplc;

 

request.getSession().setAttribute("apurl", apurl);

model.addAttribute("apurl", apurl);

System.out.println("上传图片预览路径====:" + apurl);

 

// 数据库保存路径

String saveURL = "/images/companyphoto/" + saveplc;

request.getSession().setAttribute("savePath", savePath);

request.getSession().setAttribute("saveURL", saveURL);

 

}

}

 

return "/operate/particulars";

}

 

/**

* 信息列表

*/

@RequestMapping("/listmsg")

public String listmsg(Model model, String ps,String area,HttpSession session) {

 

 

Advertises advertises = new Advertises();

if (area!=null) {

String va =null;

try {

va = new String(area.getBytes("iso-8859-1"), "utf-8");

advertises.setOfficearea(va);

System.out.println("**************************va"+va);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (ps!=null) {

session.setAttribute("ps", ps);

}

 

String position = "";

try {

String psn = new String((session.getAttribute("ps").toString()).getBytes("iso-8859-1"), "utf-8");

model.addAttribute("psn", psn);

String vv=psn.substring(0, 2);

position ="%"+ vv+"%";

} catch (Exception e) {

e.printStackTrace();

}

// model.addAttribute("psn", ps);

// String vv=ps.substring(0, 2);

// position ="%"+ vv+"%";

 

advertises.setPosition(position);

 

advertises.setId(0);

List<Advertises> alladvertises = advertisesService

.alladvertisesService(advertises);

model.addAttribute("alladvertises", alladvertises);

model.addAttribute("page", 30);

 

return "/operate/listmsg";

}

 

/**

* 显示更多信息

*/

@RequestMapping("/listmsgmore")

public @ResponseBody

List<Advertises> listmsgmore(String psn, int page) {

 

String position ="%"+ psn.substring(0, 2)+"%";

 

Advertises advertises = new Advertises();

advertises.setPosition(position);

 

advertises.setId(page);

 

List<Advertises> alladvertises = advertisesService

.alladvertisesService(advertises);

return alladvertises;

}

 

/**

* @param id

* @param model

* @param request

* @return 详细的招聘信息页面

*/

@RequestMapping("companymsg")

public String companymsg(int id,Model model) {

 

Advertises advertises=new Advertises();

advertises.setId(id);

advertises = advertisesService.admsgService(advertises);

model.addAttribute("advertises", advertises);

 

advertises.setSeveraltimes(advertises.getSeveraltimes()+1);

int up=advertisesService.updateseveraltiemsService(advertises);

if (up>0) {

System.out.println("*********浏览次数更新成功***************");

} else {

System.out.println("***浏览次数******未更新***************");

}

return "/operate/companymsg";

}

/**

* @param id

* @return 去 修改信息

*/

@RequestMapping("/updateadvmsg")

public String updateadvmsg(int id,Model model){

model.addAttribute("id", id);

return "/usermsg/updateadvmsg";

}

/**

* @param advertises

* @param request

* @param model

* @return 正在 修改信息

*/

@RequestMapping("/updateadv")

public String updateadv(Advertises advertises,HttpSession session,Model model){

 

advertises.setUserId(usersAction.uid(session));

 

advertises.setId(advertises.getId());

int success = advertisesService.updatexiangxiService(advertises);

if (success>0) {

return "redirect:/usersa/informationone.html";

}

model.addAttribute("errmsg", true);

return "/usermsg/updateadvmsg";

}

/**

* @param advertises

* @param request

* @param model

* @return 删除信息

*/

@RequestMapping("/deladvmsg")

public String deladvmsg(Advertises advertises,HttpSession session,Model model){

advertises.setId(advertises.getId());

int del=advertisesService.delmsgService(advertises);

if (del==0) {

model.addAttribute("errmsg", true);

}

 

advertises.setUserId(usersAction.uid(session));

List<Advertises> listad = advertisesService.listadvService(advertises);

model.addAttribute("listad", listad);

 

return "/usermsg/informationone";

}

/**

* @param advertises

* @param request

* @param model

* @return 全部删除

*/

@RequestMapping("/deltelmsg")

public String deltelmsg(Advertises advertises,HttpSession session,Model model,String selectid){

 

String[] one=selectid.split(",");

for (String id : one) {

advertises.setId(Integer.parseInt(id));

int del=advertisesService.delmsgService(advertises);

if (del>0) {

System.out.println("***********删除成功**************");

} else {

System.out.println("***********删除失败**************");

}

}

 

 

advertises.setUserId(usersAction.uid(session));

List<Advertises> listad = advertisesService.listadvService(advertises);

model.addAttribute("listad", listad);

return "/usermsg/informationone";

}

/**

* @return 申请职位

*/

@SuppressWarnings("static-access")

@RequestMapping("/indicatetheposition")

public @ResponseBody String indicatetheposition(HttpSession session,String companyemail){

try {

int id=usersAction.uid(session);

try {

Saveresume saveresume=new Saveresume();

saveresume.setUserid(id);

saveresume=saveresumeService.saveresumesService(saveresume);

System.out.println(saveresume.getId());

try {

/* 发送邮件 */

MailSenderInfo mailInfo = new MailSenderInfo();

mailInfo.setMailServerHost("smtp.163.com");

mailInfo.setMailServerPort("25");

mailInfo.setValidate(true);

mailInfo.setUserName("13336017350@163.com");

mailInfo.setPassword("13336017350");// 您的邮箱密码

mailInfo.setFromAddress("13336017350@163.com");

mailInfo.setToAddress(companyemail);//发送给用户

mailInfo.setSubject("简历");

 

String mailcontent="<div style='margin-left:30px;font-family:'楷体';font-size:20px;font-weight:bold; '><div style='font-size:32px;padding-top:30px;padding-left:200px; padding-bottom:30px;font-weight:bold;'>简历</div><div style='padding-bottom:40px;'><div style='float:left; width:180px;text-align:right; '>标题:</div><div style='float:left;'>"+saveresume.getJbiaoti()+"</div></div><div style='padding-bottom:40px;'><div style='float:left; width:180px;text-align:right;'>姓名:</div><div style='float:left;'>"+saveresume.getJname()+"&nbsp;&nbsp;&nbsp;&nbsp;性别:"+saveresume.getJsex()+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;出生日期:"+saveresume.getJbirthday()+"</div></div><div style='padding-bottom:40px;'><div style='float:left; width:180px;text-align:right;'>电话/手机:</div><div style='float:left;'>"+saveresume.getJphone()+"</div></div><div style='padding-bottom:40px;'><div style='float:left; width:180px;text-align:right;'>工作年限:</div><div style='float:left;'>"+saveresume.getJworker()+"</div></div><div style='padding-bottom:40px;'><div style='float:left; width:180px;text-align:right;'>学&nbsp;&nbsp;历:</div><div style='float:left;'>"+saveresume.getJschool()+"</div></div><div style='padding-bottom:40px;'><div style='float:left; width:180px;text-align:right;'>期望工作区域:</div><div style='float:left;'>"+saveresume.getJarea()+"</div></div><div style='padding-bottom:40px;'><div style='float:left; width:180px;text-align:right;'>期望月薪:</div><div style='float:left;'>"+saveresume.getJmoney()+"</div></div><div style='padding-bottom:40px;'><div style='float:left; width:180px;text-align:right;'>&nbsp;自我描述:</div><div style='float:left;width:500px;margin-bottom:50px;'>"+saveresume.getJdiscribe()+"</div></div></div>";

mailInfo.setContent(mailcontent);

// 这个类主要来发送邮件

SimpleMailSender sms = new SimpleMailSender();

sms.sendHtmlMail(mailInfo);// 发送html格式

 

} catch (Exception e) {

return "发送遇到问题了,请重试一次";

}

} catch (Exception e) {

return "写简历";

}

 

} catch (Exception e) {

return "请登录";

}

 

return "恭喜你,申请成功!";

}

}

 

posted @ 2016-10-25 15:20  次序  阅读(285)  评论(0编辑  收藏  举报