文件上传

使用Java发送邮件并上传附件
本文介绍如何使用Java代码实现发送邮件功能,并且能够上传附件。通过使用MultipartFile类来处理文件上传,同时利用Spring MVC框架的@RequestMapping注解来处理HTTP请求。

java代码如下:

        @RequestMapping(value = "sendEmail")
	@ResponseBody
	public boolean sendEmail(@RequestParam(value="title") String title,
		@RequestParam(value="message") String message, 
		@RequestParam(value="attachment" , required = false) MultipartFile file) {
		
		File dest = null;
		if(!file.isEmpty()) {
		    String orgName = file.getOriginalFilename();
		    String filePath = ResourceUtils.getAppSetting("email.upload.file") + orgName;
		dest = new File(filePath);
	        try {
	            file.transferTo(dest);
	        } catch (IllegalStateException e) {
	            e.printStackTrace();
	            return false;
	        } catch (IOException e) {
	            e.printStackTrace();
	            return false;
	        }
		}
		send(title, message, dest);
		return true;
	}

jsp文件如下:

<form id="form2" action="/apple/hr/email/list/sendEmail" method="post" enctype="multipart/form-data">

<input type="text" name="title" id="title"/>

<input type="text" name="message" id="message"/>

<input type="file" name="attachment" id="attachment" />

<form>


转载于:https://my.oschina.net/u/1583086/blog/226572

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值