首页 > 编程 > Java > 正文

Java文件上传下载、邮件收发实例代码

2019-11-26 14:12:53
字体:
来源:转载
供稿:网友

文件上传下载

前台:

1. 提交方式:post

2. 表单中有文件上传的表单项: <input type=”file” />

3. 指定表单类型:

默认类型:enctype="application/x-www-form-urlencoded"
文件上传类型:multipart/form-data

FileUpload

文件上传功能开发中比较常用,apache也提供了文件上传组件!
FileUpload组件:

1. 下载源码

2. 项目中引入jar文件

commons-fileupload-1.2.1.jar 【文件上传组件核心jar包】
commons-io-1.4.jar 【封装了对文件处理的相关工具类】

使用:

public class UploadServlet extends HttpServlet {// upload目录,保存上传的资源public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/*********文件上传组件: 处理文件上传************/try {// 1. 文件上传工厂FileItemFactory factory = new DiskFileItemFactory();// 2. 创建文件上传核心工具类ServletFileUpload upload = new ServletFileUpload(factory);// 一、设置单个文件允许的最大的大小: 30Mupload.setFileSizeMax(30*1024*1024);// 二、设置文件上传表单允许的总大小: 80Mupload.setSizeMax(80*1024*1024);// 三、 设置上传表单文件名的编码// 相当于:request.setCharacterEncoding("UTF-8");upload.setHeaderEncoding("UTF-8");// 3. 判断: 当前表单是否为文件上传表单if (upload.isMultipartContent(request)){// 4. 把请求数据转换为一个个FileItem对象,再用集合封装List<FileItem> list = upload.parseRequest(request);// 遍历: 得到每一个上传的数据for (FileItem item: list){// 判断:普通文本数据if (item.isFormField()){// 普通文本数据String fieldName = item.getFieldName(); // 表单元素名称String content = item.getString(); // 表单元素名称, 对应的数据//item.getString("UTF-8"); 指定编码System.out.println(fieldName + " " + content);}// 上传文件(文件流) ----> 上传到upload目录下else {// 普通文本数据String fieldName = item.getFieldName(); // 表单元素名称String name = item.getName(); // 文件名 String content = item.getString(); // 表单元素名称, 对应的数据String type = item.getContentType(); // 文件类型InputStream in = item.getInputStream(); // 上传文件流/** 四、文件名重名* 对于不同用户readme.txt文件,不希望覆盖!* 后台处理: 给用户添加一个唯一标记!*/// a. 随机生成一个唯一标记String id = UUID.randomUUID().toString();// b. 与文件名拼接name = id +"#"+ name;// 获取上传基路径String path = getServletContext().getRealPath("/upload");// 创建目标文件File file = new File(path,name);// 工具类,文件上传item.write(file);item.delete(); //删除系统产生的临时文件System.out.println();}}}else {System.out.println("当前表单不是文件上传表单,处理失败!");}} catch (Exception e) {e.printStackTrace();}}// 手动实现过程private void upload(HttpServletRequest request) throws IOException,UnsupportedEncodingException {/*request.getParameter(""); // GET/POSTrequest.getQueryString(); // 获取GET提交的数据 request.getInputStream(); // 获取post提交的数据 *//***********手动获取文件上传表单数据************///1. 获取表单数据流InputStream in = request.getInputStream();//2. 转换流InputStreamReader inStream = new InputStreamReader(in, "UTF-8");//3. 缓冲流BufferedReader reader = new BufferedReader(inStream);// 输出数据String str = null;while ((str = reader.readLine()) != null) {System.out.println(str);}// 关闭reader.close();inStream.close();in.close();}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doGet(request, response);}}

案例:

Index.jsp

<body> <a href="${pageContext.request.contextPath }/upload.jsp">文件上传</a>    <a href="${pageContext.request.contextPath }/fileServlet?method=downList">文件下载</a> </body>

Upload.jsp

<body> <form name="frm_test" action="${pageContext.request.contextPath }/fileServlet?method=upload" method="post" enctype="multipart/form-data"><%--<input type="hidden" name="method" value="upload">--%>用户名:<input type="text" name="userName"> <br/>文件: <input type="file" name="file_img"> <br/><input type="submit" value="提交"></form></body>

FileServlet.Java

/*** 处理文件上传与下载* @author Jie.Yuan**/public class FileServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 获取请求参数: 区分不同的操作类型String method = request.getParameter("method");if ("upload".equals(method)) {// 上传upload(request,response);}else if ("downList".equals(method)) {// 进入下载列表downList(request,response);}else if ("down".equals(method)) {// 下载down(request,response);}}/*** 1. 上传*/private void upload(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {try {// 1. 创建工厂对象FileItemFactory factory = new DiskFileItemFactory();// 2. 文件上传核心工具类ServletFileUpload upload = new ServletFileUpload(factory);// 设置大小限制参数upload.setFileSizeMax(10*1024*1024); // 单个文件大小限制upload.setSizeMax(50*1024*1024); // 总文件大小限制upload.setHeaderEncoding("UTF-8"); // 对中文文件编码处理// 判断if (upload.isMultipartContent(request)) {// 3. 把请求数据转换为list集合List<FileItem> list = upload.parseRequest(request);// 遍历for (FileItem item : list){// 判断:普通文本数据if (item.isFormField()){// 获取名称String name = item.getFieldName();// 获取值String value = item.getString();System.out.println(value);} // 文件表单项else {/******** 文件上传 ***********/// a. 获取文件名称String name = item.getName();// ----处理上传文件名重名问题----// a1. 先得到唯一标记String id = UUID.randomUUID().toString();// a2. 拼接文件名name = id + "#" + name; // b. 得到上传目录String basePath = getServletContext().getRealPath("/upload");// c. 创建要上传的文件对象File file = new File(basePath,name);// d. 上传item.write(file);item.delete(); // 删除组件运行时产生的临时文件}}}} catch (Exception e) {e.printStackTrace();}}/*** 2. 进入下载列表*/private void downList(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 实现思路:先获取upload目录下所有文件的文件名,再保存;跳转到down.jsp列表展示//1. 初始化map集合Map<包含唯一标记的文件名, 简短文件名> ;Map<String,String> fileNames = new HashMap<String,String>();//2. 获取上传目录,及其下所有的文件的文件名String bathPath = getServletContext().getRealPath("/upload");// 目录File file = new File(bathPath);// 目录下,所有文件名String list[] = file.list();// 遍历,封装if (list != null && list.length > 0){for (int i=0; i<list.length; i++){// 全名String fileName = list[i];// 短名String shortName = fileName.substring(fileName.lastIndexOf("#")+1);// 封装fileNames.put(fileName, shortName);}}// 3. 保存到request域request.setAttribute("fileNames", fileNames);// 4. 转发request.getRequestDispatcher("/downlist.jsp").forward(request, response);}/*** 3. 处理下载*/private void down(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 获取用户下载的文件名称(url地址后追加数据,get)String fileName = request.getParameter("fileName");fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");// 先获取上传目录路径String basePath = getServletContext().getRealPath("/upload");// 获取一个文件流InputStream in = new FileInputStream(new File(basePath,fileName));// 如果文件名是中文,需要进行url编码fileName = URLEncoder.encode(fileName, "UTF-8");// 设置下载的响应头response.setHeader("content-disposition", "attachment;fileName=" + fileName);// 获取response字节流OutputStream out = response.getOutputStream();byte[] b = new byte[1024];int len = -1;while ((len = in.read(b)) != -1){out.write(b, 0, len);}// 关闭out.close();in.close();}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doGet(request, response);}}

邮件开发

准备工作, 环境搭建:

1. 本地搭建一个邮件服务器

易邮服务器,eyoumailserversetup.exe

2. 新建邮箱账号

张三给李四发邮件。

步骤1:

新建域名: 工具, 服务器设置, 单域名框中输入 itcast.com

步骤2:

新建邮箱账号: zhangsan@itcast.com

lisi@itcast.com

3. 安装foxmail

配置邮件发送服务器(smtp): localhost 25
邮件接收服务器(pop3): localhost 110

再新建账号,就可以接收邮件了!

注意

注意

如果是web项目,因为javaee自带的有邮件功能,可能存在问题!

我们要用自己的mail.jar文件功能! 需要删除javaee中mail包!

使用:

JavaMail开发,先引入jar文件:

activation.jar 【如果使用jdk1.6或以上版本,可以不用这个jar文件】mail.jar 【邮件发送核心包】/*** 1. 发送一封普通邮件* @author Jie.Yuan**/public class App_SendMail {@Testpublic void testSend() throws Exception {//0. 邮件参数Properties prop = new Properties();prop.put("mail.transport.protocol", "smtp"); // 指定协议prop.put("mail.smtp.host", "localhost"); // 主机 stmp.qq.comprop.put("mail.smtp.port", 25); // 端口prop.put("mail.smtp.auth", "true"); // 用户密码认证prop.put("mail.debug", "true"); // 调试模式//1. 创建一个邮件的会话Session session = Session.getDefaultInstance(prop);//2. 创建邮件体对象 (整封邮件对象)MimeMessage message = new MimeMessage(session);//3. 设置邮件体参数: //3.1 标题message.setSubject("我的第一封邮件 ");//3.2 邮件发送时间message.setSentDate(new Date());//3.3 发件人message.setSender(new InternetAddress("zhangsan@itcast.com"));//3.4 接收人message.setRecipient(RecipientType.TO, new InternetAddress("lisi@itcast.com"));//3.5内容message.setText("你好,已经发送成功! 正文...."); // 简单纯文本邮件message.saveChanges(); // 保存邮件(可选)//4. 发送Transport trans = session.getTransport();trans.connect("zhangsan", "888");// 发送邮件trans.sendMessage(message, message.getAllRecipients());trans.close();}}

带图片

/*** 带图片资源的邮件* @author Jie.Yuan**/public class App_2SendWithImg {// 初始化参数private static Properties prop;// 发件人private static InternetAddress sendMan = null;static {prop = new Properties();prop.put("mail.transport.protocol", "smtp"); // 指定协议prop.put("mail.smtp.host", "localhost"); // 主机 stmp.qq.comprop.put("mail.smtp.port", 25); // 端口prop.put("mail.smtp.auth", "true"); // 用户密码认证prop.put("mail.debug", "true"); // 调试模式try {sendMan = new InternetAddress("zhangsan@itcast.com");} catch (AddressException e) {throw new RuntimeException(e);}}@Testpublic void testSend() throws Exception {// 1. 创建邮件会话Session session = Session.getDefaultInstance(prop);// 2. 创建邮件对象MimeMessage message = new MimeMessage(session);// 3. 设置参数:标题、发件人、收件人、发送时间、内容message.setSubject("带图片邮件");message.setSender(sendMan);message.setRecipient(RecipientType.TO, new InternetAddress("lisi@itcast.com"));message.setSentDate(new Date());/***************设置邮件内容: 多功能用户邮件 (related)*******************/// 4.1 构建一个多功能邮件块MimeMultipart related = new MimeMultipart("related");// 4.2 构建多功能邮件块内容 = 左侧文本 + 右侧图片资源MimeBodyPart content = new MimeBodyPart();MimeBodyPart resource = new MimeBodyPart();// 设置具体内容: a.资源(图片)String filePath = App_2SendWithImg.class.getResource("8.jpg").getPath();DataSource ds = new FileDataSource(new File(filePath));DataHandler handler = new DataHandler(ds);resource.setDataHandler(handler);resource.setContentID("8.jpg"); // 设置资源名称,给外键引用// 设置具体内容: b.文本content.setContent("<img src='cid:8.jpg'/> 好哈哈!", "text/html;charset=UTF-8");related.addBodyPart(content);related.addBodyPart(resource);/*******4.3 把构建的复杂邮件快,添加到邮件中********/message.setContent(related);// 5. 发送Transport trans = session.getTransport();trans.connect("zhangsan", "888");trans.sendMessage(message, message.getAllRecipients());trans.close();}}

图片+附件

/*** 3. 带图片资源以及附件的邮件* @author Jie.Yuan**/public class App_3ImgAndAtta {// 初始化参数private static Properties prop;// 发件人private static InternetAddress sendMan = null;static {prop = new Properties();prop.put("mail.transport.protocol", "smtp"); // 指定协议prop.put("mail.smtp.host", "localhost"); // 主机 stmp.qq.comprop.put("mail.smtp.port", 25); // 端口prop.put("mail.smtp.auth", "true"); // 用户密码认证prop.put("mail.debug", "true"); // 调试模式try {sendMan = new InternetAddress("zhangsan@itcast.com");} catch (AddressException e) {throw new RuntimeException(e);}}@Testpublic void testSend() throws Exception {// 1. 创建邮件会话Session session = Session.getDefaultInstance(prop);// 2. 创建邮件对象MimeMessage message = new MimeMessage(session);// 3. 设置参数:标题、发件人、收件人、发送时间、内容message.setSubject("带图片邮件");message.setSender(sendMan);message.setRecipient(RecipientType.TO, new InternetAddress("lisi@itcast.com"));message.setSentDate(new Date());/** 带附件(图片)邮件开发*/// 构建一个总的邮件块MimeMultipart mixed = new MimeMultipart("mixed");// ---> 总邮件快,设置到邮件对象中message.setContent(mixed);// 左侧: (文本+图片资源)MimeBodyPart left = new MimeBodyPart();// 右侧: 附件MimeBodyPart right = new MimeBodyPart();// 设置到总邮件块mixed.addBodyPart(left);mixed.addBodyPart(right);/******附件********/String attr_path = this.getClass().getResource("a.docx").getPath();DataSource attr_ds = new FileDataSource(new File(attr_path));DataHandler attr_handler = new DataHandler(attr_ds);right.setDataHandler(attr_handler);right.setFileName("a.docx");/***************设置邮件内容: 多功能用户邮件 (related)*******************/// 4.1 构建一个多功能邮件块MimeMultipart related = new MimeMultipart("related");// ----> 设置到总邮件快的左侧中left.setContent(related);// 4.2 构建多功能邮件块内容 = 左侧文本 + 右侧图片资源MimeBodyPart content = new MimeBodyPart();MimeBodyPart resource = new MimeBodyPart();// 设置具体内容: a.资源(图片)String filePath = App_3ImgAndAtta.class.getResource("8.jpg").getPath();DataSource ds = new FileDataSource(new File(filePath));DataHandler handler = new DataHandler(ds);resource.setDataHandler(handler);resource.setContentID("8.jpg"); // 设置资源名称,给外键引用// 设置具体内容: b.文本content.setContent("<img src='cid:8.jpg'/> 好哈哈!", "text/html;charset=UTF-8");related.addBodyPart(content);related.addBodyPart(resource);// 5. 发送Transport trans = session.getTransport();trans.connect("zhangsan", "888");trans.sendMessage(message, message.getAllRecipients());trans.close();}}

以上所述是小编给大家介绍的Java文件上传下载、邮件收发实例代码 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表