By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,406 Members | 2,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,406 IT Pros & Developers. It's quick & easy.

Tomcat write directory of received files????

P: n/a
Hi everyone,
I am trying to send files with multipart/form-date. Everything is ok
with the send.
But when I am receiving the files I should specify a directory where
the files to be saved. The problem is that Tomcat saves the files into
%CATALINA_HOME%/bin directory. I am wondering why? Why not into my
application directory?
The application directory is %CATALINA_HOME%/webapps/picture.
As a parameter of directory I am giving "." which means the same
directory.
Is there another solution except giving directory
"../webapps/pictures"

Here is the jsp code that sends the files.

<%@ page contentType="text/html; charset=windows-1251" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1251">
<title>Send Multiple</title>
<body>
<h1>Page for sending files:</h1>
<form name="send" action="receive.jsp" method="post"
enctype="multipart/form-data">
<input type="text" name="text" value="Tova e text">
<p>
<input name="file1" type="file" id="file1">
</p>
<p> <input type="file" name="file2">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>

Here is the jsp code that receives the files:

<%@ page contentType="text/html; charset=windows-1251" errorPage=""
%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%--@ page import="com.oreilly.servlet.*"--%>
<%--@ page import="com.oreilly.servlet.multipart.*"--%>
<%@ page import="com.oreilly.servlet.MultipartRequest"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileR enamePolicy"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1251">
<title>Receive</title>
</head>
<body>
<h1>Receive page:</h1>
<%
String dir = request.getContextPath();
MultipartRequest multi = new MultipartRequest(request,".");
// the problem is what to specify here insetead ".", or what to do
with
// TOMCAT to save in my application directory??????
Enumeration fnames = multi.getFileNames();
while(fnames.hasMoreElements()){
String name = (String)fnames.nextElement();
out.println(name);
}

boolean isMultipart = FileUpload.isMultipartContent(request);
/* - another package for multipart/form-date using
.../webapps/picture/images
DiskFileUpload upload = new DiskFileUpload();

List items = upload.parseRequest(request);
Iterator itr = items.iterator();

while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();

// check if the current item is a form field or an uploaded
file
if(item.isFormField()) {

// get the name of the field
String fieldName = item.getFieldName();
out.println(fieldName+" "+item.getString());

// if it is name, we can set it in request to thank the user
if(fieldName.equals("name"))
request.setAttribute("msg", "Thank You: " +
item.getString());

} else {
// fullFile.getName().
out.println(item.getFieldName()+" "+item.getName());
try{
String filename = item.getName();
//out.println(filename);
//Check if the file exist
if(filename!=null&&item.getSize()>0){
File fullFile = new File(item.getName());
//File savedFile = new
File(getServletContext().getRealPath("/"),fullFile.getName());
File savedFile = new
File("../webapps/picture/images",fullFile.getName());
item.write(savedFile);
}
else out.println("No such file:
"+item.getName());//File not specified
}catch(Exception e){out.println("Exception: "+e);}
}
}
*/
%>

</body>
</html>

If anybody could help I would be THANKFUL.
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"jajoo" <ji*****@abv.bg> wrote in message
news:2b*************************@posting.google.co m...
Hi everyone,
I am trying to send files with multipart/form-date. Everything is ok
with the send.
But when I am receiving the files I should specify a directory where
the files to be saved. The problem is that Tomcat saves the files into
%CATALINA_HOME%/bin directory. I am wondering why? Why not into my
application directory?
The application directory is %CATALINA_HOME%/webapps/picture.
As a parameter of directory I am giving "." - which means - the same
directory.
Is there another solution except giving directory
"../webapps/pictures"


Tomcat is an application like any other. Any file operations it performs
will be in its current working directory unless you specify otherwise.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.