Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

file upload question  RSS feed

 
Jim Babcock
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using a file upload bean that I found at http://www.onjava.com/pub/a/onjava/2001/04/05/upload.html and have been reasonably happy with it, until I needed a variable save location. That is, users need to upload existing files, with repetitive filenames, but different directories and they need to be uploaded to these same directories on the server to prevent them overwriting one another.

It seems that multi-part data handles form data differently and you need to get it at the same time upload the file, but unless you want to hold the whole file in memory, you can't manipulate the output directory via form data.

Anyone have a work around?

Thanks,

Jimbus
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edited]
Now I got you.

But why you want the whole path on client-side. No matter if the file names are the same, you have your file boxes/fields with different names though. You can upload all the files and can be able to save all with different names in the same directory. It should not be a problem using Jakarta Commons Fileupload project. Pardon, I never used any other.
[ May 03, 2005: Message edited by: Adeel Ansari ]
 
karthik venkatesan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

We are not using any upload tool like the one u specified. We are uploading images using multipart which is ships with Java itself. I hope the below code helps you to find a way to store the files or images in the new directories. Also u can try with simple Jakarta's commons file upload.

Jakarta Commons Fileupload


package uploadimages;

/**
* @Image upload purpose in Image Maintenance Module
*/

import java.io.*;
import java.util.*;
import MultipartFormData.*;
import javax.servlet.http.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import Utilities.Utilities;
import javax.naming.*;

public class uploads extends HttpServlet {

public void init() throws javax.servlet.ServletException {
super.init();
}

public void doPost(
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {

MultipartFormData multipart = new MultipartFormData( request );
String pIdSKU = ""; StringpId = "";
// Fetches the corresponding UPC code for the Product Id entered in imagemaintenence.jsp
String suffix = "";
// Stores the randomly generated UPC suffix value
String errmsg = "";
String tempImagePath = "";
// Stores the image path in temp folder of the application
StringUserIn = "";// Keeps the name on User currentln logged in
int flag = 0;// Tri-Bry // To check the Preview-Upload control flow
int IdValidityFlag = 0;
// Checks the validity of the Id
int DataValidityFlag = 0;
// For "Browse" text field validation

/*
* For anti-caching and changing image reference from SKU # to UPC # */

HttpSession session = request.getSession(false);
if(session != null)
{
Enumeration attributeNames = session.getAttributeNames();
while(attributeNames.hasMoreElements())
{
String attributeName = (String)attributeNames.nextElement();
if(attributeName.equals("suffix")) {
suffix = (String)session.getAttribute(attributeName);
}
if(attributeName.equals("UserIn")) {
UserIn = (String)session.getAttribute(attributeName);

}
}
try {

pIdSKU = multipart.getParameter("prodid");
pIdSKU=pIdSKU.toUpperCase().trim();
System.err.println("pIdSKU--"+pIdSKU);
DBBean db = new DBBean();
db.connect();
String sql= "Select MMUPCS From ALCOFILES.MSTITEM where MMITM# ='"+pIdSKU+"'";
System.err.println("sql--"+sql);
ResultSet rs = db.executeQuery(sql);

if (rs.next())
{
int pIdInt=rs.getInt("MMUPCS");
pId=String.valueOf(pIdInt);

Context ctx = new InitialContext();

String imagedir = (String) ctx.lookup("java:comp/env/IMAGE_DIR");
String imagedirR = (String) ctx.lookup("java:comp/env/REPLACED_IMAGE_DIR");
System.err.println("imagedir--"+imagedir);
System.err.println("imagedirR--"+imagedirR);
File destDir = new File(imagedir);
File destDirR = new File(imagedirR);

String fname=multipart.getFileName( "newfile" );
System.err.println("fname by multipart--"+fname);
File FName = new File(fname);

if(DataValidityFlag != 1) {
if (( fname.endsWith("jpg")==true)||(fname.endsWith("JPG")==true))
{
try
{
File RDir = new File(alcoimagedirR);
String FList[] = RDir.list();
System.err.println("flist length--"+FList.length);
for(int i=0;i<FList.length;i++)
{
String fileName = FList[i];
System.err.println("file name--"+fileName);
if(fileName.indexOf('_') != -1)
{
String itemId = fileName.substring(0, fileName.indexOf('_'));
if(itemId.equals(pId)) {
suffix = fileName.substring(fileName.indexOf('_')+1, fileName.lastIndexOf('.'));
}
}
}
}
catch(Exception e)
{
System.out.println("caught exception" +e);
}
System.err.println("newName--"+pId+".jpg");
System.err.println("newNameR--"+pId + "_" + UserIn + "_" + suffix + ".jpg");
File newName = new File( destDir, pId+".jpg" );
File newNameR = new File( destDirR, pId + "_" + UserIn + "_" + suffix + ".jpg" );

if(newName.exists()){newName.delete();System.err.println("new Name deleted");}
if(newNameR.exists()){newNameR.delete();System.err.println("new Name R deleted");}

NameGenerator lNg = new NameGenerator();
suffix = lNg.RandomGenerator();
System.err.println("suffix--"+suffix);
newNameR = new File( destDirR, pId + "_" + UserIn + "_" + suffix + ".jpg" );

File tempPath = new File( multipart.getLocalPath( "newfile" ) );
System.err.println("temppath--"+ tempPath);
boolean abc=tempPath.renameTo( newName );
System.err.println("bool value--"+abc);
System.err.println("initiate file copy");
System.err.println("newName--"+pId+".jpg");
System.err.println("newNameR--"+pId + "_" + UserIn + "_" + suffix + ".jpg");

FileCopy fc = new FileCopy();
fc.copy(newName, newNameR);
System.err.println("--copied--");
session.setAttribute("suffix", suffix);
session.setAttribute("sameId", new Integer(11));
}
else
{
DataValidityFlag = 1;
}
}
ctx.close();
ctx = null;
}
else
{
DataValidityFlag = 1;
}
rs.close();
db.close();
rs = null;
db = null;
} catch (Exception e) {
errmsg= e.getMessage();
}

multipart.cleanUp();

//-To update Associated Image Flag
Utilities.updateAssociatedImageFlag(pIdSKU,pId);

RequestDispatcher rd =
getServletContext().getRequestDispatcher("/imagemaintenance.jsp?errmsg="+errmsg+"&prodid="+pIdSKU+"&Flag=1"+"&DataValidityFlag="+DataValidityFlag+"&IdValidityFlag="+IdValidityFlag);
if (rd != null) {
rd.forward(request,response);
}
System.out.println("****END UPLOADS.JAVA*****");
}
}


public void destroy() {

super.destroy();
}

public void doGet(
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
doPost(request,response);
}

}
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
Another issue to consider is that not all browsers send the full path of the file being uploaded. MSIE does and I think recent versions of Firefox do.
Netscape and at least one fairly recent version of Mozilla that I know of only send the filename. This could further complicate your efforts.

-Ben

[ May 03, 2005: Message edited by: Bear Bibeault ]
[ May 03, 2005: Message edited by: Ben Souther ]
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think o'reilly multipart can do what is required here. i have used it, not for the purpose you want to, but i can specify output directories etc...and, like Ben mentioned, that package is exremely popular/robust/reliable.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, remember, you are at the mercy of the browsers if your code requires a full path. Some will only send the filename.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can handle it by concatenating the timestamp with the filename.
or
Just store the file in your database.

Am I on the same page? I am worried because thinking that it should not be an issue.
[ May 03, 2005: Message edited by: Adeel Ansari ]
 
Jim Babcock
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, Jakarta Commons Fileupload did the trick. Sorry I didn't do such a good job presenting the issue, but my experience has been, if you provide details, people question your methods instead of anwering the question... That's a many years and many forums, not one from this forum in particular... sometimes I go to far the other way, not giving enough or clear info.

I work for a cellco and the guy across from me is responsibile for the error messages or DRAMS and management wanted to make them available on line. There is 20-30 of the same recordings for each of our 12 or so switches all with names like "Track05_MSG_24.pcm". I need to figure out how to upload them into a base directory with the switch name inserted between the base and the filename. The switch name comes from a pulldown in the same form as filename.

The original bean I found, which if you do a google search for "java file upload" the jakarta stuff isn't there, does everything at once, so it needs a static path. After sleeping on it over night, I was considering writing it as a temp file and then moving it once I had the final path, but the Jakarta piece had it set up nicely.

Thanks again... I wonder how much work it would be to do multiple files... doesn't look like much .

Jimbus
 
Jim Babcock
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, new issue: I can't make the file selector in IE choose more than one file... ctl-a, ctl-click shift-click... all choose one file

Any ideas?

Thanks,

Jimbus
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a new input tag for each file.
(with a little javascript driven DHTML, you can allow the user to create as many as they need without making a trip to the server)
[ May 03, 2005: Message edited by: Ben Souther ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See mail.yahoo.com for attachments.
See mail.lycos.co.uk for attachments.

You can also have a look on geocities file manager file upload for multiple file uploading.

Both have different ways to allow multiple files upload.
Thanks.
[ May 04, 2005: Message edited by: Adeel Ansari ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!