文件上传

需要的jar包:

commo.io;

文件上传要求:

1.为了文件的安全性,我们要把文件放在外界无法直接访问的目录下;

2.要限制文件的大小

3.为了防止发生重名覆盖的现象,我们要避免重名

4.可以限制文件的类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.saxon.file;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class fileUpLoad extends HttpServlet {

@Override
protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//判断文件是不是有文件上传的项
if (! ServletFileUpload.isMultipartContent (req)) {
return;//终止方法,没有文件上传的功能
}
//创建文件上传的路径 建议在WEB-INF目录下因为外部无法直接访问,比较安全;
String realPath = this.getServletContext ().getRealPath ("/WEB-INF/load");
File file = new File (realPath);
if (! file.exists ()) {
boolean mkdir = file.mkdir ();
}
//临时文件区域,当文件的大小超过了一定的大小,就放在这里,超过一定的时间九自动删除;
String tempPath = this.getServletContext ().getRealPath ("/WEB-INF/temp");
File temp = new File (tempPath);
if (! temp.exists ()) {
boolean mkdir = temp.mkdir ();
}
//获得一个工厂
/*
@see DiskFileItemFactory
* public static final int DEFAULT_SIZE_THRESHOLD = 10240;
* private File repository; 存储库文件的地方
* private int sizeThreshold; 文件的大小
* private FileCleaningTracker fileCleaningTracker;清理文件
* private String defaultCharset;编码
* public static final String DEFAULT_CHARSET = "ISO-8859-1";
*/
DiskFileItemFactory factory = new DiskFileItemFactory ();
//factory要有。
ServletFileUpload Upload = new ServletFileUpload ();
//监听我们的进程的进度
Upload.setProgressListener (new ProgressListener () {
/**
* @param pBytesRead 已经读取的文件
* @param pContentLength 文件上传的大小
* @param pItems 已经上传的项目
*/
@Override
public void update (long pBytesRead, long pContentLength, int pItems) {
}
});
//获得文件的后缀和文件的名字

//解析前端的请求,封装成一个fileitem对象
List<FileItem> list = null;
try {
list = Upload.parseRequest (req);
for (FileItem fileItem : list) {
if (fileItem.isFormField ()) {

}
}
} catch (FileUploadException e) {
e.printStackTrace ();
}
}

@Override
protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet (req, resp);
}
}

邮件发送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.saxon.mail;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class mail {
//配置文件
public static void main (String[] args) throws Exception {
Properties properties = new Properties ();
//准备
properties.setProperty ("mail.host", "smtp.qq.com");//设置QQ邮箱
properties.setProperty ("mail.transport.protocol", "smtp");//设置发送协议
properties.setProperty ("mail.smtp.auth", "true");//是否验证密码
//QQ邮箱

MailSSLSocketFactory socketFactory = new MailSSLSocketFactory ();
socketFactory.setTrustAllHosts (true);
properties.put ("mail.smtp.ssl.enable", "true");
properties.put ("mail.smtp.ssl.socketFactory", socketFactory);


//邮箱的五个步骤
//1.设置session环境
Session defaultInstance = Session.getDefaultInstance (properties, new Authenticator () {
@Override
protected PasswordAuthentication getPasswordAuthentication () {
return new PasswordAuthentication ("2433027822@qq.com", "ffvluhisjcfzebab");//前一个写地址,后一个写验证码
}
});
defaultInstance.setDebug (true);
//2获得transport对象.用来发送邮件
Transport ts = defaultInstance.getTransport ();
//3.使用邮箱和授权码来进行连接
ts.connect ("smtp.qq.com", "2433027822#qq.com", "ffvluhisjcfzebab");
//4,编写邮件
Message message = new MimeMessage (defaultInstance);
//指明发件人
message.addRecipient (Message.RecipientType.TO, new InternetAddress ("2433027822@qq.com"));
//设置主题
message.setSubject ("你好啊");
//设置图片内容
MimeBodyPart image = new MimeBodyPart ();
//图片要处理一下数据
DataHandler dataHandler = new DataHandler (new FileDataSource ("D:\\dnf补丁\\bg.jpg"));
image.setDataHandler (dataHandler);
image.setContentID ("bg.jpg");//这里设置的ID我们子啊后面可以接着使用
//准备正文数据
MimeBodyPart content=new MimeBodyPart ();
content.setContent ("这是一个带有图片的邮件<img src='uid:bg.jpg'>","text/html;charset=utf-8");
//描述数据关系
MimeMultipart mm=new MimeMultipart ();
mm.addBodyPart (image);
mm.addBodyPart (content);
mm.setSubType ("related");
message.setText ("这是第一份邮件");

//设置到消息中
message.setContent (mm);//把最后消息发送到消息中
message.saveChanges ();//保存修改
//设置接受的人
message.setFrom (new InternetAddress ("2433027822@qq.com"));
//5.发送邮件
ts.sendMessage (message,message.getAllRecipients ());
ts.close ();
}

}