[英]How to access variable from another class
I'm new to java, and i cant figure how to access a variable from another class我是 java 新手,我不知道如何从另一个类访问变量
I am trying to write a code that sends a mail with attachment which is not stored locally.And i want to access a variable written in ExcelFile Class in SendMail Class我正在尝试编写一个代码来发送带有附件的邮件,该附件未在本地存储。我想访问在 SendMail 类的 ExcelFile 类中编写的变量
public class ExcelFile {
public static void generateExcelFile(String fileName,Map<int, String> studentRecord){
XSSFWorkBook workbook = new XSSFWorkbook();
XSSFSheet sheet= workbook.createSheet();
writeHeaderLines(sheet);//method that writes the header lines for the excel file
writeDataLines(studentRecord,sheet);//method that writes the data into the excel file
ByteArrayOutputStream bos=new ByteArrayOutputStream();
try{
workbook.write(bos);}
finally{
bos.close();
}
byte[]excelFileAsByte = bos.toByteArray();// trying to refer this in SendMail Class
workbook.close();
}}
public class SendMail{
public void sendMailWithAttachment(String excelFileName){
try{
String recipient = "recipient@gmail.com";
String sender = "sender@gmail.com";
String host = "127.0.0.1";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
message.setSubject("This is Subject");
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("This is body of the mail");
BodyPart messageBodyPart2 = new MimeBodyPart();
//DataSource source = new FileDataSource(filename);
//messageBodyPart2.setDataHandler(new DataHandler(source));
ByteArrayResource resource = new ByteArrayResource(excelFileAsByte);
messageBodyPart2.setFileName(excelFileName);
Multipart multipartObject = new MimeMultipart();
multipartObject.addBodyPart(messageBodyPart1);
multipartObject.addBodyPart(messageBodyPart2);
message.setContent(multipartObject);
Transport.send(message);
System.out.println("Mail successfully sent");
}
catch (MessagingException mex)
{
mex.printStackTrace();
}
}}
how can i access excelFileAsByte in the other class and send the mail without storing it locally.我如何在其他类中访问 excelFileAsByte 并发送邮件而不将其存储在本地。 And Can i add the file as an attachment using the addBodyPart and ByteArrayResource.
我可以使用 addBodyPart 和 ByteArrayResource 将文件添加为附件吗?
You will need a field.您将需要一个字段。 This is a very common thing in Java.
这在 Java 中是很常见的事情。 Add below to the class ExcelFile.
将下面添加到 ExcelFile 类中。 This is known as property setters and getters.
这被称为属性设置器和获取器。
public class ExcelFile {
private byte[] excelFileAsByte;
public byte[] getExcelFileAsByte() {
return excelFileAsByte;
}
public void setExcelFileAsByte(byte[] excelFileAsByte) {
this.excelFileAsByte = excelFileAsByte;
}
...
}
And also:并且:
static
from generateExcelFile.static
。byte[]excelFileAsByte = bos.toByteArray()
to excelFileAsByte = bos.toByteArray()
. byte[]excelFileAsByte = bos.toByteArray()
更改为excelFileAsByte = bos.toByteArray()
。 In your other class, access it with在您的其他课程中,使用
ExcelFile excelFile = new ExcelFile();
excelFile.generateExcelFile(...
byte[] myExcelFile = excelFile.getExcelFileAsByte();
This is not a complete example, but I hope it will get you started.这不是一个完整的示例,但我希望它能帮助您入门。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.