How to use OPA form to upload files?
Hi!
I was tring to upload file using OPA form. I would like to ask two questions
1. Wheather my approch is correct or not
2. error I am facing
Approch
I made a custom control and used OnRequestEventHandler in custom control the
<input type="text" was changed to
<input type="form"
and changes were made to form.vm
where
*<form name="form" accept-charset="UTF-8" method="POST" action="${post-uri}" target="${frameset-top-target}">*
was changed to
*<form name="form" ENCTYPE="multipart/form-data" accept-charset="UTF-8" method="POST" action="${post-uri}" target="${frameset-top-target}">*
and wrote the following Java Code
2. error
Due to change in form.vm
OPA throws a null pointer exception
Please help either solving the solution of my problem or giving me a workaround thanks
bellow is java code I used also
_________________________java code________________________
package com.aosf.handlers;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import com.oracle.determinations.web.platform.eventmodel.events.OnRequestEvent;
import com.oracle.determinations.web.platform.eventmodel.handlers.OnRequestEventHandler;
import com.oracle.determinations.web.platform.plugins.WebDeterminationsServletPlugin;
import com.oracle.determinations.web.platform.plugins.WebDeterminationsServletRegisterArgs;
public class getFile implements OnRequestEventHandler {
public void handleEvent(Object arg0, OnRequestEvent arg1) {
// TODO Auto-generated method stub
HttpServletRequest request = arg1.getHttpRequest();
String contentType = arg1.getHttpRequest().getContentType();
System.out.println("###getFile:"+contentType);
if((contentType != null) && contentType.indexOf("multipart/form-data") >= 0){
try {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
System.out.println("Contentlength:"+formDataLength);
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String (dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"")+10);
saveFile = saveFile.substring(0,saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
System.out.println("dataBytes"+dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex+1,contentType.length());
System.out.println(boundary);
int pos;
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;
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes,startPos,(endPos - startPos));
fileOut.flush();
fileOut.close();
System.out.println("File saved as"+saveFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public WebDeterminationsServletPlugin getInstance(
WebDeterminationsServletRegisterArgs arg0) {
// TODO Auto-generated method stub
return new getFile();
}
}
__________________Java code end___________________