2012-03-29 2 views
0

파일 업로드를 허용하도록 Liferay Web Form 포틀릿을 사용자 정의하려고하는데 전송할 이메일에 파일을 첨부하는 것 외에는 거의 모든 작업을 수행했습니다. 양식에서commons-fileupload 및 Liferay 문제 해결

얻기 분야 :

public void saveData(ActionRequest actionRequest, ActionResponse actionResponse) { 

... 

File uploadedFile = null; 

... 

Map<String,String> fieldsMap = new LinkedHashMap<String,String>(); 

// Create a FileItemFactory for disk-based file items 
DiskFileItemFactory factory = new DiskFileItemFactory(1073741824, new File("/temp")); 

// Create a new file upload handler 
PortletFileUpload upload = new PortletFileUpload(factory); 
upload.setSizeMax(67108864); 

// Parse the request 
List<FileItem> items = upload.parseRequest(actionRequest); 

int i = 1; 

Iterator iter = items.iterator(); 

// Throw away the hidden field, don't need it 
FileItem dud = (FileItem)iter.next(); 

while (iter.hasNext()) { 

    FileItem item = (FileItem)iter.next(); 

    String fieldLabel = preferences.getValue(
     "fieldLabel" + i, StringPool.BLANK); 

    String fieldType = preferences.getValue(
      "fieldType" + i, StringPool.BLANK); 

    if (Validator.isNull(fieldLabel)) { 
     break; 
    } 

    if(!fieldType.equals("file")) { 
     String fieldValue = item.getString(); 
     fieldsMap.put(fieldLabel, fieldValue); 
    } else { 
     String fieldName = item.getName(); 
     uploadedFile = new File(fieldName); 
     item.write(uploadedFile); 
    } 
     i++; 
} 
... 

    if(sendAsEmail) { 
     emailSuccess = sendEmail(fieldsMap, preferences, uploadedFile); 
    } 
} 

그리고 건설 및 이메일의 전송 :

protected boolean sendEmail(Map<String, String> fieldsMap, PortletPreferences preferences, File uploadedFile) { 

    MailMessage mailMessage = new MailMessage(fromAddress, toAddress, subject, body, false); 

    if(uploadedFile != null) { // i.e., there was 'file' field up above 
     mailMessage.addAttachment(uploadedFile); 
    } 

    MailServiceUtil.sendEmail(mailMessage); 


    if(uploadedFile != null) { 
     uploadedFile.delete(); 
    } 

} 

나는 파일 업로드를 처리하고 첨부하려고 다음과 같은 콘솔 오류 파일에 메시지 :

16:09:49,597 ERROR [MailEngine:489] IOException while sending message 
16:09:49,598 ERROR [MailEngine:154] java.io.FileNotFoundException: helpdesk_.png (No such file or directory) 

나는을 가지고 있습니까?3210 올바르게 구성 되었습니까? 나는 무엇을 잘못 할 수 있 었는가?

감사합니다.

답변

0

MailServiceUtil 하위 시스템이 실제 전자 메일을 보내도록 스레드를 돌리고 그 메시지가 반환 될 때까지 기다리지 않습니다 (따라서 스레드). 그래서 MailServiceUtil.sendEmail()을 호출 한 후, 나는 실제로 보낼 기회가 오기 전에 첨부 할 파일을 즉시 삭제하고있었습니다!

파일 객체를 전달할 수있는 MailServiceUtil의 래퍼 유형을 살펴볼 것입니다. 그런 다음 실제로는 전송을 수행하면됩니다.

0

당신이 MailEngine.send 방법을

보내기 (사용해 볼 수 있습니다 InternetAddress InternetAddress []에,에서, InternetAddress [] CC, InternetAddress [] 숨은, InternetAddress [] bulkAddresses, 문자열 제목, 문자열 몸, 부울 htmlFormat, InternetAddress [] ReplyTo의, 문자열 messageId가, 문자열 inReplyTo, 파일 [] 첨부)

난 당신이 이미가 전송 발견으로 문제를 밖으로 정렬하는 경우 MailEngineUtil.sendEmail에도 작동하는지 확실하지 않다 버스를 타다가 청취자를 잃는다. d는 mailMessage에 추가하더라도 첨부 파일을 읽지 않는 (MailMessageListener라고 생각합니다.) 호출됩니다.