fileupload problem with seam
sunkariramesh Feb 11, 2011 8:18 AMHi,
I am using s:fileupload tag in jsp and my task is to upload a file and need to persist and sending that file as attachement to the selected mail id's
But i am getting error with this code can any one help out this problem...here i am sending my jsp file and action class and error
Action:---
public void confirmNotification() {
this.confirmHotels();
if(validateMail() && validateMobile()){
if(serviceAlerts.getId() == 0) {
this.serviceAlerts.setNotificationType(adminNotificationDetails.getNotificationType());
this.serviceAlerts.setModifiedUser(user.getUsername());
this.serviceAlerts.setMessageType(adminNotificationDetails.getMessageType());
this.serviceAlerts.setModifiedDate(new Date());
this.serviceAlerts.setCreatedDate(new Date());
this.serviceAlerts.setStartDate(adminNotificationDetails.getStartDate());
this.serviceAlerts.setCreatedUser(user.getUsername());
if(adminNotificationDetails.getNotificationType()!=null){
if(adminNotificationDetails.getNotificationType().equals(ServiceAlerts.NotificationType.Custom)){
this.serviceAlerts.setCustomNotificationName(adminNotificationDetails.getCustomNotificationName());
}}
if(adminNotificationDetails.getMessageType() !=null){
if((adminNotificationDetails.getMessageType().equals(ServiceAlerts.MessageType.Email)) || ( adminNotificationDetails.getMessageType().equals(ServiceAlerts.MessageType.EmailAndSMS))){
this.serviceAlerts.setAddEmails(adminNotificationDetails.getAddEmails());
this.serviceAlerts.setEmailContent(adminNotificationDetails.getEmailContent());
this.serviceAlerts.setEmailSubject(adminNotificationDetails.getEmailSubject());
}
if((adminNotificationDetails.getMessageType().equals(ServiceAlerts.MessageType.SMS)) || ( adminNotificationDetails.getMessageType().equals(ServiceAlerts.MessageType.EmailAndSMS))){
this.serviceAlerts.setAddSmss(adminNotificationDetails.getAddSmss());
this.serviceAlerts.setSmsContent(adminNotificationDetails.getSmsContent());
}}
int formDataLength = adminNotificationDetails.getSize();
byte[] dataBytes = new byte[formDataLength];
adminNotificationDetails.setEmailAttachment(dataBytes);
this.saveFile();
this.serviceAlerts.setEmailAttachment(adminNotificationDetails.getEmailAttachment());
this.serviceAlerts.setSendFrequency(adminNotificationDetails.getSendFrequency());
this.selectHotels();
em.persist(serviceAlerts);
System.out.println("bytes is -------------"+serviceAlerts.getEmailAttachment());
}
else
{
this.serviceAlerts.setModifiedDate(new Date());
this.serviceAlerts.setModifiedUser(user.getUsername());
}}
// if(this.serviceAlerts.getStartDate()!=null && this.serviceAlerts.getSendFrequency()!=0)
sendConfirmationMail();
em.flush();
}
public FileOutputStream saveFile(){
try {
DataInputStream in = new DataInputStream(adminNotificationDetails.getFile());
int formDataLength1 = adminNotificationDetails.getSize();
byte dataBytes[] = adminNotificationDetails.getEmailAttachment();
System.out.println("size is --------"+formDataLength1);
String file = new String(dataBytes);
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength1) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength1);
totalBytesRead += byteRead;
}
//for saving the file name
String saveFile = file.substring(file.indexOf("filename=\"") +10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
int lastIndex = adminNotificationDetails.getContentType().lastIndexOf("=");
String boundary = adminNotificationDetails.getContentType().substring(lastIndex + 1,adminNotificationDetails.getContentType().length());
int pos;
//extracting the index of file
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
// creating a new file with the same name and writing the content in new file
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
System.out.println("the file name is --------"+saveFile);
return fileOut;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
Jsp:----
<s:fileUpload id="uploadFile"
data="#{adminNotificationDetails.file}"
fileName="#{adminNotificationDetails.name}"
fileSize="#{adminNotificationDetails.size}"
contentType="#{adminNotificationDetails.contentType}"
accept="images/*,application/*" />
Structure:----
@Name("adminNotificationDetails")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
public class AdminNotificationDetails {
private NotificationType notificationType;
private MessageType messageType;
private String customNotificationName;
private String emailSubject;
private String emailContent;
private String smsContent;
private String userName;
private Date startDate;
private int sendFrequency;
private String addSmss;
private String addEmails;
private String toEmails;
private byte[] emailAttachment;
private int size;
private String name;
private InputStream file;
private String contentType;
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getEmailAttachment() {
return emailAttachment;
}
public void setEmailAttachment(byte[] emailAttachment) {
this.emailAttachment = emailAttachment;
}
public String getSmsContent() {
return smsContent;
}
public void setSmsContent(String smsContent) {
this.smsContent = smsContent;
}
public String getAddSmss() {
return addSmss;
}
public void setAddSmss(String addSmss) {
this.addSmss = addSmss;
}
public String getAddEmails() {
return addEmails;
}
public void setAddEmails(String addEmails) {
this.addEmails = addEmails;
}
public int getSendFrequency() {
return sendFrequency;
}
public void setSendFrequency(int sendFrequency) {
this.sendFrequency = sendFrequency;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public String getEmailContent() {
return emailContent;
}
public void setEmailContent(String emailContent) {
this.emailContent = emailContent;
}
public String getEmailSubject() {
return emailSubject;
}
public void setEmailSubject(String emailSubject) {
this.emailSubject = emailSubject;
}
public String getCustomNotificationName() {
return customNotificationName;
}
public void setCustomNotificationName(String customNotificationName) {
this.customNotificationName = customNotificationName;
}
public NotificationType getNotificationType() {
return notificationType;
}
public void setNotificationType(NotificationType notificationType) {
this.notificationType = notificationType;
}
public MessageType getMessageType() {
return messageType;
}
public void setMessageType(MessageType messageType) {
this.messageType = messageType;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getToEmails() {
return toEmails;
}
public void setToEmails(String toEmails) {
this.toEmails = toEmails;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public InputStream getFile() {
return file;
}
public void setFile(InputStream file) {
this.file = file;
}
}
I am using s:fileupload tag in jsp and my task is to upload a file and need to persist and sending that file as attachement to the selected mail id's
But i am getting error with this code can any one help out this problem...here i am sending my jsp file and action class and error
Action:---
public void confirmNotification() {
this.confirmHotels();
if(validateMail() && validateMobile()){
if(serviceAlerts.getId() == 0) {
this.serviceAlerts.setNotificationType(adminNotificationDetails.getNotificationType());
this.serviceAlerts.setModifiedUser(user.getUsername());
this.serviceAlerts.setMessageType(adminNotificationDetails.getMessageType());
this.serviceAlerts.setModifiedDate(new Date());
this.serviceAlerts.setCreatedDate(new Date());
this.serviceAlerts.setStartDate(adminNotificationDetails.getStartDate());
this.serviceAlerts.setCreatedUser(user.getUsername());
if(adminNotificationDetails.getNotificationType()!=null){
if(adminNotificationDetails.getNotificationType().equals(ServiceAlerts.NotificationType.Custom)){
this.serviceAlerts.setCustomNotificationName(adminNotificationDetails.getCustomNotificationName());
}}
if(adminNotificationDetails.getMessageType() !=null){
if((adminNotificationDetails.getMessageType().equals(ServiceAlerts.MessageType.Email)) || ( adminNotificationDetails.getMessageType().equals(ServiceAlerts.MessageType.EmailAndSMS))){
this.serviceAlerts.setAddEmails(adminNotificationDetails.getAddEmails());
this.serviceAlerts.setEmailContent(adminNotificationDetails.getEmailContent());
this.serviceAlerts.setEmailSubject(adminNotificationDetails.getEmailSubject());
}
if((adminNotificationDetails.getMessageType().equals(ServiceAlerts.MessageType.SMS)) || ( adminNotificationDetails.getMessageType().equals(ServiceAlerts.MessageType.EmailAndSMS))){
this.serviceAlerts.setAddSmss(adminNotificationDetails.getAddSmss());
this.serviceAlerts.setSmsContent(adminNotificationDetails.getSmsContent());
}}
int formDataLength = adminNotificationDetails.getSize();
byte[] dataBytes = new byte[formDataLength];
adminNotificationDetails.setEmailAttachment(dataBytes);
this.saveFile();
this.serviceAlerts.setEmailAttachment(adminNotificationDetails.getEmailAttachment());
this.serviceAlerts.setSendFrequency(adminNotificationDetails.getSendFrequency());
this.selectHotels();
em.persist(serviceAlerts);
System.out.println("bytes is -------------"+serviceAlerts.getEmailAttachment());
}
else
{
this.serviceAlerts.setModifiedDate(new Date());
this.serviceAlerts.setModifiedUser(user.getUsername());
}}
// if(this.serviceAlerts.getStartDate()!=null && this.serviceAlerts.getSendFrequency()!=0)
sendConfirmationMail();
em.flush();
}
public FileOutputStream saveFile(){
try {
DataInputStream in = new DataInputStream(adminNotificationDetails.getFile());
int formDataLength1 = adminNotificationDetails.getSize();
byte dataBytes[] = adminNotificationDetails.getEmailAttachment();
System.out.println("size is --------"+formDataLength1);
String file = new String(dataBytes);
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength1) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength1);
totalBytesRead += byteRead;
}
//for saving the file name
String saveFile = file.substring(file.indexOf("filename=\"") +10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
int lastIndex = adminNotificationDetails.getContentType().lastIndexOf("=");
String boundary = adminNotificationDetails.getContentType().substring(lastIndex + 1,adminNotificationDetails.getContentType().length());
int pos;
//extracting the index of file
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
// creating a new file with the same name and writing the content in new file
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
System.out.println("the file name is --------"+saveFile);
return fileOut;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
Jsp:----
<s:fileUpload id="uploadFile"
data="#{adminNotificationDetails.file}"
fileName="#{adminNotificationDetails.name}"
fileSize="#{adminNotificationDetails.size}"
contentType="#{adminNotificationDetails.contentType}"
accept="images/*,application/*" />
Structure:----
@Name("adminNotificationDetails")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
public class AdminNotificationDetails {
private NotificationType notificationType;
private MessageType messageType;
private String customNotificationName;
private String emailSubject;
private String emailContent;
private String smsContent;
private String userName;
private Date startDate;
private int sendFrequency;
private String addSmss;
private String addEmails;
private String toEmails;
private byte[] emailAttachment;
private int size;
private String name;
private InputStream file;
private String contentType;
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getEmailAttachment() {
return emailAttachment;
}
public void setEmailAttachment(byte[] emailAttachment) {
this.emailAttachment = emailAttachment;
}
public String getSmsContent() {
return smsContent;
}
public void setSmsContent(String smsContent) {
this.smsContent = smsContent;
}
public String getAddSmss() {
return addSmss;
}
public void setAddSmss(String addSmss) {
this.addSmss = addSmss;
}
public String getAddEmails() {
return addEmails;
}
public void setAddEmails(String addEmails) {
this.addEmails = addEmails;
}
public int getSendFrequency() {
return sendFrequency;
}
public void setSendFrequency(int sendFrequency) {
this.sendFrequency = sendFrequency;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public String getEmailContent() {
return emailContent;
}
public void setEmailContent(String emailContent) {
this.emailContent = emailContent;
}
public String getEmailSubject() {
return emailSubject;
}
public void setEmailSubject(String emailSubject) {
this.emailSubject = emailSubject;
}
public String getCustomNotificationName() {
return customNotificationName;
}
public void setCustomNotificationName(String customNotificationName) {
this.customNotificationName = customNotificationName;
}
public NotificationType getNotificationType() {
return notificationType;
}
public void setNotificationType(NotificationType notificationType) {
this.notificationType = notificationType;
}
public MessageType getMessageType() {
return messageType;
}
public void setMessageType(MessageType messageType) {
this.messageType = messageType;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getToEmails() {
return toEmails;
}
public void setToEmails(String toEmails) {
this.toEmails = toEmails;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public InputStream getFile() {
return file;
}
public void setFile(InputStream file) {
this.file = file;
}
}