Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

struts.upload

Status
Not open for further replies.

Ssanai80

IS-IT--Management
Jun 28, 2001
21
0
0
US
I am trying use struts.upload api to create a small app to upload a file and write it to a specific folder. It seems like my app is doing everything but writing to the physical drive.

Can anyone see why the file is not being written to the /repository folder under webroot?

fyi, i'm using jdk 1.4 and tomcat 4.1

here is the UploadFile.java that should write the file to the directory specified but does not. I've highlighted the part that seems to not work:


import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io_OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class UploadFile extends Action {

// --------------------------------------------------------- Instance Variables

// --------------------------------------------------------- Methods

/**
* Method execute
* @param ActionMapping mapping
* @param ActionForm form
* @param HttpServletRequest request
* @param HttpServletResponse response
* @return ActionForward
* @throws Exception
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

UploadForm theForm = (UploadForm) form;
//retrieve the file representation
FormFile file = theForm.getTheFile();
//retrieve the file name
String fileName = file.getFileName();

//retrieve the content type
String contentType = file.getContentType();
System.out.println(contentType); // temporary debugging

//retrieve the file size
String size = (file.getFileSize() + " bytes");
String data = null;

try {
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();

//write the file to the file specified
OutputStream bos = new FileOutputStream("/repository");
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();

data =
"The file has been written to \""
+ "/repository"
+ "\"";
}
//close the stream
stream.close();
} catch (FileNotFoundException fnfe) {
return null;
} catch (IOException ioe) {
return null;
}

return mapping.findForward("display");
}
}
 
Hi,

Try this

FileOutputStream bos = new FileOutputStream(new File(getServletContext().getRealPath("/repository"), fileName));

Cheers
Venu
 
Venu,

Thank you for your reply. Your suggestion did not work. But I was able to get my codes working by writing the full path of the destination path instead of "/repository".

replacing "/repository" with "C:/tomcat/webapps/project/repository/ + fileName" did the trick.

I'm not sure why the relative path did not work though.

Thanks,

spark
 
Hi,

Try to print this and see it should print the path

getServletContext().getRealPath("/repository/")

Cheers
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top