cancel
Showing results for 
Search instead for 
Did you mean: 

File Transfer with Servlet

Former Member
0 Kudos

hi,

i need to download files from server context root to client side using servlet !!!

is it possible ????

regards

Guru

Accepted Solutions (1)

Accepted Solutions (1)

former_member182294
Active Contributor
0 Kudos

Hi,

Its possible to download files to client side. Copy the files to your servlet context and provide them as Hyper Link.

Regards

Abhilash

Former Member
0 Kudos

hi,

i hava done as said .but it is not working ...

error is:requested resource does not exists.

i have typed url like : http://192.168.1.76:50000/download/xxx pgf

can u pls tell how to use servlet inputstree and out put stree to read and write files

regards

Guru

Former Member
0 Kudos

Hi Guru,

You need to restart the server and then it will work

But let me try it myself and will let you know.

Best regards,

Guru.

former_member182294
Active Contributor
0 Kudos

Hi Guru,

Where you copied the PDF file? Copy files into <b>webContent</b> folder in NWDS.

Then rebuild the Web Application, Enterprise Application and deploy it. Check the context root in application.xml, in this case your context root name should be <b>download</b>.

Regards

Abhilash

Former Member
0 Kudos

hi,

it is displaying content in browser. but i need down load that file in client ,i need to give option to user to where he want down load that file in his system.

regards

Guru

Former Member
0 Kudos

Well if the pdf is being displayed in the browser wouldn't the pdf plugin give him the "Save as" file save button to allow him to save the downloaded pdf locally ???

Former Member
0 Kudos

hi,

actually my requirement is : i need to upload files to server0 folder (in was) and down load files from that folder .

regards

Guru

former_member182294
Active Contributor
0 Kudos

Hi Guru,

I think it depends on the ActiveX controls and secrurity settings in the browser.

The other options is provide hyper links for all the documents and user can use <b>Save Target As</b> Option to save the file.

Regards

Abhilash

former_member182294
Active Contributor
0 Kudos

Hi Guru,

Here is the solution, check the code. This code can be used for word, excel,ppt, pdf file types. If you need any other files then add them in if condtions.

Add the following code into your service or doPost or doGet method. Here you need not to keep the files under web context folder. You can keep folders any where in the system and you can refer it.

////////////////////

String fileName = "test.pdf";

String filePath = "g:
";

String fileType = fileName.substring(fileName.lastIndexOf("."),fileName.length());

response.setHeader("Content-Disposition","attachment; filename="+fileName);

if (fileType.trim().equalsIgnoreCase("doc"))

{

response.setContentType( "application/msword" );

}

else if (fileType.trim().equalsIgnoreCase("xls"))

{

response.setContentType( "application/vnd.ms-excel" );

}

else if (fileType.trim().equalsIgnoreCase("pdf"))

{

response.setContentType( "application/pdf" );

}

else if (fileType.trim().equalsIgnoreCase("ppt"))

{

response.setContentType( "application/ppt" );

}

else

{

response.setContentType( "application/octet-stream" );

}

FileInputStream fis = new FileInputStream(filePath+fileName);

byte in[] = new byte[512];

response.setContentLength(fis.available());

OutputStream stream = response.getOutputStream();

while(fis.read(in)>0)

stream.write(in);

stream.flush();

stream.close();

///////////////

Regards

Abhilash

Former Member
0 Kudos

hi,

thanque very much for ur response.

every thing is ok. but file is saving with servliet name (by default)

regards

Guru

former_member182294
Active Contributor
0 Kudos

Did you pass the file name in the response header?

response.setHeader("Content-Disposition","attachment; filename="+fileName);

Regards

Abhilash

Former Member
0 Kudos

hi,

i passed the file namein respone header

regards

Guru

Former Member
0 Kudos

hi,

up

file is saving in servlet url name?????////

regards

Guru

former_member182294
Active Contributor
0 Kudos

Guru,

I tried it, its working very fine. I am able to save the file with the name given the Header. I really dont know where your doing wrong, can you post your code?

Regards

Abhilash

Former Member
0 Kudos

hi

pls check code once

	//String fileName = (String) request.getParameter("file");
			String fileName = "\one.doc";
			String filePath = "D:\usr\sap\J2E\JC00\j2ee\cluster\server0";

			String fileType = fileName.substring(fileName.lastIndexOf("."),fileName.length());




regards
Guru
			response.setHeader("Content-Disposition","attachment; filename="+fileName);
			 

			if (fileType.trim().equalsIgnoreCase("doc"))
			{
			response.setContentType( "application/msword" );
			}
			else if (fileType.trim().equalsIgnoreCase("xls"))
			{
			response.setContentType( "application/vnd.ms-excel" );
			}
			else if (fileType.trim().equalsIgnoreCase("pdf"))
			{
			response.setContentType( "application/pdf" );
			}
			else if (fileType.trim().equalsIgnoreCase("ppt"))
			{
			response.setContentType( "application/ppt" );
			}
			
			else if (fileType.trim().equalsIgnoreCase("jar"))
									{
									response.setContentType( "application/jar" ); 
									}
			

			else if (fileType.trim().equalsIgnoreCase("cbc"))
									{
									response.setContentType( "application/cbc" ); 
									}
			
			else 
			{
			response.setContentType( "application/octet-stream" ); 
			}
			FileInputStream fis = new FileInputStream(filePath+fileName);
			byte in[] = new byte[512];
			response.setContentLength(fis.available());
			OutputStream stream = response.getOutputStream();
		
			while(fis.read(in)>0)
			stream.write(in);

			stream.flush();
			stream.close();


}

}

Former Member
0 Kudos

hi ,

i found the mistake

regards

Guru

former_member182294
Active Contributor
0 Kudos

Change this way..

String fileName = "one doc";

String filePath = "D:
usr
sap
J2E
JC00
j2ee
cluster
server0
";

Regards

Abhilash

Former Member
0 Kudos

hi,

thanque very much for ur response.

i have another requirment that is :there is xml file in server0 folder. it need to download when client call this servlet <b>with out asking</b> save or open option

is it possible?it need to be stored in default path.

how to find system default?

regards

Guru

former_member182294
Active Contributor
0 Kudos

Guru,

If you want to directly open the XML file you can place that file under context root or remove the content disposition and header information from previous code. If you want to achieve all things in a single servlet then based on the file type set the header and content disposition.

Generally system default path will be Temp files. If you want to find the path you can use java.io.File class.

Regards

Abhilash

Former Member
0 Kudos

hi,

i need to store xml file in temp dir without asking client to choose path when call servlet.

is it possible?

regards

Guru

former_member182294
Active Contributor
0 Kudos

I dont think its possible through Servlet. Might be possible with Applets.

Regards

Abhilash

Former Member
0 Kudos

hi,

can u guid me how to achive this using applets

regards

Guru

former_member182294
Active Contributor
0 Kudos

Using Applets you need to write coding using io package which is like normal file access. But only thing you need to consider is you need to set java policy file with proper permissions.

Regards

Abhilash

Former Member
0 Kudos

hi,

1.how to use javapolicy file ?

2 in which package i can find java policy file?

3. which values i need to set?

regards

Guru

former_member182294
Active Contributor
0 Kudos

Check this <a href="http://java.sun.com/j2se/1.3/docs/guide/security/PolicyFiles.html">Java Policy</a>

Regards

Abhilash

Former Member
0 Kudos

hi,

Abhilash

just i have seen ur previous posts about sales order ..

i have some doubts , can u explain?

<b>in BAPI_SALESORDER_CREATEFROMDAT2</b>

1.i set the doc_type is "TA" in headerin

2.i set the part_role is "AG" in partner

3.i setupdate flag is "I" in headerinx.............................X,X,X...??

<b>my doubt is wht are the meaning to TA,AG,I ...etc and how to know the meanings??</b>

regards

Guru

Former Member
0 Kudos

hi,

up

up

up

servlet is working fine. but the problem is , it is displaying content in browser when i call this servlet url from<b> mobile browser</b>. where as it is working fine wheni call servlet url from system.

mobile is WIN C. is there any setting i need to do in mobile.

regards

Guru

Message was edited by:

Guruvulu Bojja

Answers (0)