cancel
Showing results for 
Search instead for 
Did you mean: 

Webddynpro Java 2004s: I would like save file in local directory

0 Kudos

Hi Experts,

I like to use upload file UI element in in Developer studio developement.

In NWDS7.0 just we have resource type data element to map for that, we dont hava binary.

My requiremnt is : I need to upload file and save in network direcotry and i also i need to read file when it requires.

Below code executing but not creating file in directory. could you pleas help me to choose correct API to save and read file in network directory with little bit code.

IPrivatePercompView.IContextElement element = wdContext.currentContextElement();

File file = new File("
'network\'Shared\'files\file''");

InputStream input;

int temp;

FileOutputStream op = new FileOutputStream(file);

if (element.getFileResource() != null) {

IWDResource resource = element.getFileResource();

input = wdContext.currentContextElement().getFileResource().read(false);

while((input.read())!=-1)

{

temp = input.read();

op.write(temp);

}

op.flush();

op.close();

}

}catch(Exception e)

{

wdComponentAPI.getMessageManager().reportException(e.getMessage(),false);

}

Thanks

Ramana

Accepted Solutions (0)

Answers (6)

Answers (6)

Former Member
0 Kudos

Hi,

For export file in format XML, TXT, ...

continue steps

1) create node with name  FileResource and type binary

2) create view and add control filedownload and properties control set data binding node FileResource

3) create following code

  //@@begin javadoc:wdDoInit()

    /** Hook method called to initialize controller. */

  //@@end

  public void wdDoInit()

  {

    //@@begin wdDoInit()

        IWDAttributeInfo attInfo = wdContext.getNodeInfo().getAttribute(IPrivateExportListView.IContextElement.FILE_RESOURCE);

        IWDModifiableBinaryType binaryType = (IWDModifiableBinaryType) attInfo.getModifiableSimpleType();  

        binaryType.setFileName(ExportListView.FILE_NAME);

        binaryType.setMimeType(WDWebResourceType.TXT);

        try {

            String resourcePath = WDURLGenerator.getResourcePath(wdComponentAPI.getDeployableObjectPart(), ExportListView.FILE_NAME);

            wdContext.currentContextElement().setFileResource(this.getByteArrayFromResourcePath(resourcePath));

        } catch (WDAliasResolvingException e) {

            wdComponentAPI.getMessageManager().reportException(e.getLocalizedMessage(), true);

        } catch (Exception e) {

            throw new WDRuntimeException(e);

        }

    //@@end

  }

and add following code

  //@@begin others

    private byte[] getByteArrayFromResourcePath(String resourcePath) throws FileNotFoundException, IOException {

        FileInputStream in = new FileInputStream(new File(resourcePath));

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int length;

        String Prueba = "hola esto es una prueba" + new Date().getSeconds();

        //byte[] part = new byte[10 * 1024];

        byte[] part = Prueba.getBytes();

        while ((length = in.read(part)) != -1) {

            out.write(part, 0, length);

        }

        out.write(Prueba.getBytes());

        in.close();

        return out.toByteArray();

    }

    // store image file name in constant FILE_NAME

    private static final String FILE_NAME = "doc.txt";

  //@@end

4)Create file ext(txt,xml,...) in following dir of the project

...\_comp\src\mimes\Components\com.prueba.ReporteComp

regards

from : Medellín-Colombia

0 Kudos

Solved my self

Former Member
0 Kudos

Hi Ramana,

Below are the steps required for file download.

1. Create a context node say cntxt_FileResource of type com.sap.ide.webdynpro.uielementdefinitions.Resource

2. Now create the file Download UI element and bind the Data and resource property with cntxt_FileResource .

3. Now you can use the below code for downloading the file example is for .txt file change it according to your requirement..

InputStream inputStream =
					new FileInputStream("C:\\Puneet\\uploading2.txt");//Path of file stored in server
				IWDResource resource2 =
					WDResourceFactory.createResource(
						inputStream,
						"name of file with extention like uploading2.txt",
						WDWebResourceType.TXT,
						true);
	// Set the path in the context node 							wdContext.currentContextElement().setCntxt_FileResource(
					resource2);
				wdContext
					.currentContextElement()
					.setCntxt_FileDownLoadBehaviour(
					WDFileDownloadBehaviour.ALLOW_SAVE);

Please revert if you require more info.

Regards

Narendra

0 Kudos

Hi Narendra,

File is creating in my UNIX server folder like. /usr/sap/HRK/JC72/j2ee/cluster/server0/file.txt.

After that I want read that file. While time of reading I donu2019t know file extension.

my code: is working only TXT files,

Actually I donu2019t k now file extension at time of reading. Just I know file name. How can I achieve this?, to read any type of file. Please look and help me.

InputStream stream = new FileInputStream("/usr/sap/HRK/JC72/j2ee/cluster/server0/"wdContext.currentContextElement().getFille()".txt");

IWDResource resource = WDResourceFactory.createResource(stream, wdContext.currentContextElement().getFile(),

WDWebResourceType.TXT, true);

wdContext.currentContextElement().setFileResourceD(resource);

Thanks

Ramana

Former Member
0 Kudos

Please have a look at the below thread

0 Kudos

Hi ,

I need to read from server directory.

that thread does't helped.

Please send pice of code to read from directory to context.

Thanks

Ramana

sanyev
Active Participant
0 Kudos

Hi Ramana,

For reading a file from the server location you can use normal java io read. try this [link|http://www.exampledepot.com/egs/java.io/File2ByteArray.html]. Only thing that you need to take care is to use the file path in this format.

"C:	empoutfilename.txt"

Once you have the file as a byte array you can use this [tutorial|https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/202a850a-58e0-2910-eeb3-bfc3e081257f] to download the file.

Regards,

Sanyev

Former Member
0 Kudos

Try this code.


IWDResource resource = null;
InputStream stream;
	
try {
	stream = new FileInputStream( <path>);
	resource = WDResourceFactory.createResource( stream, name, 
					WDWebResourceType.UNKNOWN, true);
	stream.close();
        wdComponentAPI.getWindowManager()
		.createNonModalExternalWindow( resource.getUrl( 0), name).show();	
	
} catch (FileNotFoundException e) {
} catch (IOException e) {
}

vinod v

Former Member
0 Kudos

Hi Ramana,

For Saving file in your local directory folder you have to use file download UI element as you can't directly create a file in your local folder.

Since your application is running on server so what you can do is by using file upload save the file in server directory folder and then by using file doenload UI element save that file in your local system.

Hope this will help.

Regards

Narendra

0 Kudos

Hi Narendra,

Thanks for you replay,

Can ypu please send pice of code to read file from server to context. for download process.

Thanks

Venkat

sanyev
Active Participant
0 Kudos

Hi Ramana,

Your code to write the contents of the IWDResouce to a file looks fine. Ideally this piece of code should work. Can you check if you have write permission on the
'network\'Shared\'files\file''" folder. You can also try mapping the shared folder to a drive and then using the drive letter instead of
'network\'Shared\'files\file''".

I hope you have given the sharing rights on the server. The application runs on the server so the permissions to access the folder should be given to the server machine. It doesn't matter if your local system has access to the network folder or not.

if you fix the problem by your own, do post on what the problem was.

Sanyev

0 Kudos

Hi Sanyev,

Thanks for you reply.

If I change to my local direcotry, it is not working like "C:
file.txt".

It is wrting file in server0 folder.

like user\...\server0\..

and also creationg file with name of "C:
file.txt" in server. it is not at all hitting my local or network folder. I do have access for network folder too.

I dont what is happening.

And alos i want read this file .

Could you please send any pice of code to read from server0 folder.

totaly i need to write and read file at any folder location(net work is good to me).

Please help me.

Thanks

Ramana

sanyev
Active Participant
0 Kudos

Hi Ramana,

This is a piece of code that I used to write debug info to a file and I know that it works.

try {
        BufferedWriter out = new BufferedWriter(new FileWriter("C:\\temp\\outfilename.txt", true));
        out.write("***Init***");
        out.close();
    } catch (IOException e) {
    }

The file will be on the server at the location c:\temp\outfilename.txt

i guess you have to use
instead of \ so your networkfolder would look something like this.

"\\\\network\\Shared\\files\\file\\data.txt"

Hope it helps,

Sanyev