Retain Uploaded File

Hi,

I am using FileUpload API to send a file to a server. The thing is that the file is sent but it is automatically deleted by this API. i would like to know how to retain the file permanently in the location to which it is uploaded.

Thanks

[260 byte] By [subhashmedhia] at [2007-11-27 11:30:31]
# 1

Post your code? It the file deleted by the server or the client? When is it deleted?

Kaj

kajbja at 2007-7-29 16:34:25 > top of Java-index,Java Essentials,Java Programming...
# 2

package uploadApp;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.*;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.*;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UseFileUpload extends HttpServlet{

private static final long serialVersionUID = 1;

public void doPost(HttpServletRequest req, HttpServletResponse res) {

try{

//Check that we have a file upload request

boolean isMultipart = ServletFileUpload.isMultipartContent(req);

if(isMultipart){

//Create a factory for disk-based file items

DiskFileItemFactory factory = new DiskFileItemFactory();

//Set factory constraints

factory.setSizeThreshold(10*1024*1024);

factory.setRepository(new File("C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\UploadGui\\store"));

//Create a new file upload handler

ServletFileUpload upload = new ServletFileUpload(factory);

//Set overall request size constraint

upload.setSizeMax(10*1024*1024);

//Parse the request

List /* FileItem */ items = upload.parseRequest(req);

//Process the uploaded items

Iterator iter = items.iterator();

while (iter.hasNext()) {

FileItem item = (FileItem) iter.next();

if (item.isFormField()) {

//Process a regular form field

String name = item.getFieldName();

System.out.println("Name of the input text field is" + name);

String value = item.getString();

System.out.println("Input text is" + value);

}

else {

//Process a file upload

String fieldName = item.getFieldName();

System.out.println("Field Name " + fieldName);

String fileName = item.getName();

/*

//Tokenize the Filename string to get the last part

String[] result = fileName.split("\\s");

int i = result.length;

String real_filename = result[i -1];

*/

System.out.println("FileName is" + fileName);

String contentType = item.getContentType();

System.out.println("Content type is " + contentType);

boolean isInMemory = item.isInMemory();

if(isInMemory){

System.out.println("Item is in memory");

/*

//Copy the file(any format) stored in memory into a designated location

File fin = new File("C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\UploadGui\\store\\real_filename");

File fout = new File("C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\UploadGui\\real\\real_filename");

InputStream in = new FileInputStream(fin);

OutputStream out = new FileOutputStream(fout);

byte[] buf = new byte[1024];

int len;

while ((len = in.read(buf)) > 0) {

out.write(buf, 0, len);

}

in.close();

out.close();

*/

}

long sizeInBytes = item.getSize();

System.out.println("Size of the item is" + sizeInBytes);

}

}

}

}catch(Exception e){

e.printStackTrace();

}

}

}

subhashmedhia at 2007-7-29 16:34:25 > top of Java-index,Java Essentials,Java Programming...
# 3

Possible cause could be that u missed closing the output stream

New_Kida at 2007-7-29 16:34:25 > top of Java-index,Java Essentials,Java Programming...
# 4

you can use the below code

String contentType = request.getContentType();

if(null != contentType && contentType.toLowerCase().startsWith("multipart")){

try{

File mkd=null;

mkd=new File("your path");

mkd.mkdirs();

MultipartRequest multi=new MultipartRequest(request,"your path");

}

catch(Exception e)

{}

}

deepagangaa at 2007-7-29 16:34:25 > top of Java-index,Java Essentials,Java Programming...
# 5

> catch(Exception e) {}

Sure.

CeciNEstPasUnProgrammeura at 2007-7-29 16:34:25 > top of Java-index,Java Essentials,Java Programming...
# 6

> > catch(Exception e) {}

>

> Sure.

:)

kajbja at 2007-7-29 16:34:25 > top of Java-index,Java Essentials,Java Programming...
# 7

Returning back to my original program, when i don't input any text in the html form field and select a file named(path included here) "C:\Documents and Settings\esubmed\Desktop\About ClearCase_files\btn_s

el_page32.1177107725.gif"

for uploading i get the output as below:

Name of the input text field istextline

Input text is

Field Name datafile

FileName isC:\Documents and Settings\esubmed\Desktop\About ClearCase_files\btn_s

el_page32.1177107725.gif

Content type is image/gif

Item is in memory

java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomca

t 5.5\webapps\UploadGui\real\real_filename (The system cannot find the path spec

ified)

at java.io.FileOutputStream.open(Native Method)

at java.io.FileOutputStream.<init>(Unknown Source)

at java.io.FileOutputStream.<init>(Unknown Source)

at uploadApp.UseFileUpload.doPost(UseFileUpload.java:91)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl

icationFilterChain.java:269)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF

ilterChain.java:188)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV

alve.java:210)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV

alve.java:174)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j

ava:127)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j

ava:117)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal

ve.java:108)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav

a:151)

at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcesso

r.java:834)

at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.pr

ocess(Http11AprProtocol.java:640)

at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:12

86)

at java.lang.Thread.run(Unknown Source)

What could be the reasons for such an output?

I forgot to mention that i am using the portions to copy the file which had been commented earlier.

Message was edited by:

subhashmedhi

subhashmedhia at 2007-7-29 16:34:25 > top of Java-index,Java Essentials,Java Programming...
# 8

> What could be the reasons for such an output?

What do you think?

java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\UploadGui\real\real_filename (The system cannot find the path specified)

kajbja at 2007-7-29 16:34:25 > top of Java-index,Java Essentials,Java Programming...
# 9

when u r submitting,set the content type as multipart,then u wil get

deepagangaa at 2007-7-29 16:34:25 > top of Java-index,Java Essentials,Java Programming...
# 10

<form action="urservlet" enctype="multipart/form-data" name="" method="POST">

<input type="file" name="filename" />

<input type="submit" name="Submit" value="Submit" />

</form>

here "filename" is the temperarily file name of the file uploading ,use this name in the servlet what i send the code early.

put the cos.jar file in the library folder

deepagangaa at 2007-7-29 16:34:25 > top of Java-index,Java Essentials,Java Programming...