Hi All,
I am new to SAP MII and I searched a lot for the file upload on SCN, I also saw a couple of code snippets.
I tried using the Apache APIs, but couldn't make it work.
I have a code that I borrowed from a related thread, I just would like to ask how is the below code of JSP getting the filename ?
<%@ page import = "java.io.*" %>
<%@ page import = "java.util.*"%>
<%@ page import = "java.net.URLEncoder"%>
<%@ page import = "javax.servlet.*"%>
<%@ page import = "javax.servlet.http.*"%>
<%@ page import = "javax.naming.*"%>
<%@ page import = "javax.sql.*"%>
<%@ page import = "java.sql.*"%>
<% String contentType = request.getContentType();
if( (contentType != null) && (contentType.indexOf("multipart/form-data") >= 0 ) )
{
DataInputStream in = new DataInputStream( request.getInputStream() );
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[ formDataLength ];
int byteRead = 0;
int totalBytesRead = 0;
byteRead = in.read( dataBytes, 0 ,formDataLength );
String httpPostContent = new String( dataBytes );
String fileName = httpPostContent.substring( httpPostContent.indexOf("fileName=\"")+10);
fileName = fileName.substring( 0, fileName, indexOf("\n") );
fileName = fileName.substring( fileName.lastIndexOf("\\") + 1, fileName.indexOf( "\"" ));
int length = dataBytes.length;
int lastIndex = contentType.lastIndexOf( "=" );
String boundary = contentType.substring( lastIndex + 1, contentType.length() );
int pos;
pos = httpPostContent.indexOf( "fileName=\"");
pos = httpPostContent.indexOf( "\n", pos) + 1;
pos = httpPostContent.indexOf( "\n", pos) + 1;
pos = httpPostContent.indexOf( "\n", pos) + 1;
int boundaryLocation = httpPostContent.indexOf( boundary, pos ) - 4;
int startPos = ( ( httpPostContent.substring( 0, pos )).getBytes()).length;
int endPos = ( ( httpPostContent.substring( 0, boundaryLocation )).getBytes()).length;
String output = URLEncoder.encode( httpPostContent.substring( startPos, endPos ));
String site = ("/XMII/Runner?Transaction=411186/WriteFileTrx&InFileName=" + fileName);
response.sendRedirect(site);
}
%>
Regards
Anish Vyas
After a lot of digging and searching. I managed to write the code for it. It includes Apache's FileUpload API.
Code to help others :
<%@ page import="java.util.*" %>
<%@ page import="java.io.File" %>
<%@ page import="java.lang.Exception" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.DiskFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%
// Check that we have a file upload request
boolean isMultipart = DiskFileUpload.isMultipartContent(request);
// Create a factory for disk-based file items
DefaultFileItemFactory factory = new DefaultFileItemFactory();
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload(factory);
// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {} else
{
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
out.println("fieldName = " + fieldName);
out.println("contentType = " + contentType);
out.println("fileName = " + fileName);
out.println("isInMemory = " + isInMemory);
out.println("sizeInBytes = " + sizeInBytes);
String fileContent = item.getString();
out.println(fileContent);
String site = ("/XMII/Runner?Transaction=411186/WriteFileTrx&InFileName=" + fileName + "&InFileContent=" + fileContent);
response.sendRedirect(site);
}
}
%>
Hello Anish,
You cant directly upload files in SAP MII workbench except Import / Export of MII workbench. If you are using SAPUI5 as web page, you can use FileUpload control. This control will allow to select file from local drive in running time and it will give encrypted output for that file. You can pass this content as input to transaction in MII and you can decrypt in transaction by using transaction expressions and you can save it in MII workbench.
Thanks,
Senthil
Hi anish,
I have not tried to upload file using jsp. but still after checking your code i can see you are not mapping the encoded file output to the transaction. check your code again.
hope u can fix it.
regards,
Akalya
Hi Akalya,
I checked my code and I get what you are saying. I changed the Runner code to
String site = ("/XMII/Runner?Transaction=411186/WriteFileTrx&InFileName=" + fileName+"&InFileContent="+output);
which now includes the encoded file content.
I just have one more query regarding the code.
String fileName = httpPostContent.substring(httpPostContent.indexOf("fileName=\"")+10);
fileName = fileName.substring( 0, fileName, indexOf("\n") );
fileName = fileName.substring( fileName.lastIndexOf("\\") + 1, fileName.indexOf( "\"" ));
From what I could understand from the above code is, it is trying to fetch the file name. Is that correct?
If so, I am unable to perform this.It gives me ArrayOutOfBoundException. Can you please help me out with this?
Regards
Anish Vyas
Add a comment