Skip to Content
author's profile photo Former Member
Former Member

File Upload from Local drive to MII WEB Folder

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

Add a comment
10|10000 characters needed characters exceeded

Related questions

4 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Aug 17, 2016 at 11:28 AM

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

    }

    }

    %>

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 16, 2016 at 09:34 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

    • Akalya Natarajan Senthil kumar Thangavel

      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

  • Posted on Aug 16, 2016 at 12:28 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 17, 2016 at 06:26 AM

    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
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.