cancel
Showing results for 
Search instead for 
Did you mean: 

File Upload from Local drive to MII WEB Folder

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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);

            }

    }

%>

Answers (3)

Answers (3)

Former Member
0 Kudos

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

0 Kudos

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

senthil_kumar44
Participant
0 Kudos

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

0 Kudos

hi senthil,

Am also trying the same, what u have mentioned above. but am not clear in  mapping the selected file to transaction expression.

could u please explain a bit more?

regards,

Akalya

senthil_kumar44
Participant
0 Kudos

Hello Akalya,

In FileUpload control, when we select the file, it will modify that file into encrypted format. In transaction, set string input parameter and assign this encoded value into transaction string parameter. Once we received encoded value inside transaction, we can decode it and save.

We have done this for uploading data in CSV files from local drive to MDO database. It worked successfully.

Thanks,

Senthil

Former Member
0 Kudos

Hi Senthil,

I am not using SAPUI5, I am using a basic file type input from HTML5 using it to pass the file in multipart form to a JSP file where I am encoding the input and targeting it to a Input parameter in a transaction.

By looking into different posts regarding File Upload , I got to know the process. I was confused with Multi-part file transfer. I just have a few syntax errors. Then I'll be good to go. Still, thank you.

For reference :

Regards

Anish Vyas

0 Kudos

thanks senthil.

actually now we are trying to upload excel data to MDO DB. I have used string_list_to_xml_parser action block in the transaction while giving inputs manually for the transaction in excel format it works.

now i want pass the input to the transaction using ui5 file uploader but i dont know how to assign the excel data to transaction parameter.

can you please share your ui5 code for my reference?

regards,

Akalya