Re: Upload to Directory 2.0m6
()) { attachmentId = rs.getInt(autoId); } } } catch (Exception e) { myLog.info(Exception: + e.getMessage()); } finally { try { if (ps != null) { ps.close(); } if (ds != null) { ds.closeConnection(conn); } } catch (Exception e) {} } // here's where things get dicey. the binarystream upload can fail without an exception being thrown. // so we'll test the blob's length to determine if the upload succeeded, just to be sure try { sqlString = SELECT contents FROM attachments WHERE attachment_id=?; conn = ds.getConnection(); ps = StatementFactory.getStatement(conn, sqlString, cmi); ps.setInt(1, attachmentId); ResultSet rs = ps.executeQuery(); if (rs.next()) { is = rs.getBinaryStream(contents); fileLength = is.available(); } } catch (Exception e) { myLog.info(Exception: + e.getMessage()); } finally { try { if (ps != null) { ps.close(); } if (ds != null) { ds.closeConnection(conn); } if (is != null) { is.close(); } // close parameter inStream in calling function } catch (Exception e) { } } return (fileLength == 0)?-1:attachmentId; } We use this code to allow users to upload files that can be attached to other info we are storing. For example, you can attach a po pdf to a purchase order. The code runs in a Tomcat servlet environment, so myResource has a servlet mapping in web.xml that points to the Restlet application that the router code is part of. Be sure to include the org.apache.commons.io.jar file. CMInfo is used to log user info on database updates, but it is not vital to the example. We're not using Restlet logging since we already had been using log4j for our other servlets. -- View this message in context: http://n2.nabble.com/Upload-to-Directory-tp2438898p4191642.html Sent from the Restlet Discuss mailing list archive at Nabble.com. -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2431725
Re: Upload to Directory
Hi Christian, I use the following restlet to save ANY uploaded file with whatever extension or format it is ... hope it worx n solves your problem my friend /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package resources; /** * * @author M.Abdel-Aziz */ import org.restlet.resource.Resource; import org.restlet.Context; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; import org.restlet.resource.Representation; import org.restlet.resource.StringRepresentation; import org.restlet.resource.Variant; import java.sql.*; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import java.io.*; import java.util.Calendar; import java.util.*; import cdf.capabilities.common.*; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.restlet.ext.fileupload.RestletFileUpload; public class FileUploader extends Resource { String dateRequested = null; public FileUploader(Context context, Request request, Response response) { super(context, request, response); // Allow modifications of this resource via POST requests setModifiable(true); getVariants().add(new Variant(MediaType.MULTIPART_ALL)); } @Override public void handleGet() { Representation rep = new StringRepresentation(invoke me with POST to upload your file, MediaType.TEXT_PLAIN); getResponse().setEntity(rep); } @Override public void acceptRepresentation(Representation entity) { try { uploadFile(entity, requesterIP); } catch (IOException ioex) { System.out.println(Oops...IOException !!!); } } public void uploadFile(Representation file, String reqIp) throws IOException { FileItemFactory factory = new DiskFileItemFactory(); RestletFileUpload fileUpload = new RestletFileUpload(factory); List list = null; try { list = fileUpload.parseRepresentation(file); } catch (FileUploadException ex) { System.out.println(Oops...FileUploadException !!!); } Iterator iter = list.iterator(); String fileName = null; String destination = null; if (iter.hasNext()) { try { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) { String uploadedFileName = item.getName(); File itemFile = new File(uploadedFileName); // creating destination directory on the server File destDir = new File(target_folder_here); if (!destDir.exists()) { destDir.mkdirs(); } fileName = itemFile.getName(); destination = destDir.getAbsolutePath(); File destFile = new File(destDir, itemFile.getName()); //writing into destination file. item.write(destFile); insertData(fileName, reqIp); getResponse().setEntity(302: file received successfully, MediaType.TEXT_PLAIN); } } catch (Exception ex) { System.out.println(Oops...EXCEPTION !!!); } } } } Regards ... -- View this message in context: http://n2.nabble.com/Upload-to-Directory-tp2438898p2604760.html Sent from the Restlet Discuss mailing list archive at Nabble.com. -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1595300
RE: Upload to Directory
Hi Christian, For the asdf extension, you can simply register it with the MetadataService. For example: myApplication.getMetadataService().addExtension(asdf, MediaType.valueOf(yourNew/mimeType)); For your second case, I'm not sure. As a workaround, you could register the bin extension with MediaType#APPLICATION_OCTET_STREAM. I would suggest entering a new RFE if you really need to upload something without extension. Maybe we could guess the extension in this case based on the posted entity's media type of fallback on a default bin extension. Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com -Message d'origine- De : Christian Haintz [mailto:christian.hai...@gmail.com] Envoyé : dimanche 8 mars 2009 12:47 À : discuss@restlet.tigris.org Objet : Re: Upload to Directory Hello Thierry, I played around with that but i didn't find a solution that fits my needs. I just want to be able to upload _any_ type of file to the Directory Object. Eg.: myPDF.pdf myTXT.txt myOwnFormat.asdf myFileWithoutExtension I succeed with the first two, but what about the last two. There is no MimeType for that registered and so is no MediaType. Is there a way to achieve this i am not able to see, or is it just not supported. Thank you. Best regards, Christian Haintz On Mar 7, 2009, at 8:53 AM, Thierry Boileau wrote: Hello Christian, there is a mechanism based on the MetadaService [1] that checks the media type of the uploaded file with the one deduced from the URI of the resource. For instance, you put a file with mediatype */* on a URI such as http://www.example.com/fille.txt. Based on the metadata service, the media type of the resource is text/plain. The rule is : if */* is not included into text/plain then, reject the uploaded representation. it avoid to send a binary file when a text file is expected. Having said that, this mechanism could be optional, it isn't. best regards, Thierry Boileau Hi, I simply wanna upload a File to the org.restlet.Directory. But all I get is: See Other (303) - The metadata are not consistent with the URI I don't know what that means, or what I can do against it. On the Server Side i do: Application application = new Application() { @Override public Restlet createRoot() { Directory dir = new Directory(getContext(), ROOT_URI); dir.setModifiable(true); dir.setDeeplyAccessible(true); return dir; } }; return application; And the client which is uploading a file looks: Client client = new Client(Protocol.HTTP); Representation rep = new FileRepresentation(fileOpt.getValue(), MediaType.ALL); Response response = client.put(reference, rep); Any suggestions? Thx, christian -- Christian Haintz Student of Software Development and Business Management Graz, University of Technology -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageI d=1279726 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId =1281460 -- Christian Haintz Student of Software Development and Business Management Graz, University of Technology -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=12885 92 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1317161
Re: Upload to Directory
Hello Thierry, I played around with that but i didn't find a solution that fits my needs. I just want to be able to upload _any_ type of file to the Directory Object. Eg.: myPDF.pdf myTXT.txt myOwnFormat.asdf myFileWithoutExtension I succeed with the first two, but what about the last two. There is no MimeType for that registered and so is no MediaType. Is there a way to achieve this i am not able to see, or is it just not supported. Thank you. Best regards, Christian Haintz On Mar 7, 2009, at 8:53 AM, Thierry Boileau wrote: Hello Christian, there is a mechanism based on the MetadaService [1] that checks the media type of the uploaded file with the one deduced from the URI of the resource. For instance, you put a file with mediatype */* on a URI such as http://www.example.com/fille.txt. Based on the metadata service, the media type of the resource is text/plain. The rule is : if */* is not included into text/plain then, reject the uploaded representation. it avoid to send a binary file when a text file is expected. Having said that, this mechanism could be optional, it isn't. best regards, Thierry Boileau Hi, I simply wanna upload a File to the org.restlet.Directory. But all I get is: See Other (303) - The metadata are not consistent with the URI I don't know what that means, or what I can do against it. On the Server Side i do: Application application = new Application() { @Override public Restlet createRoot() { Directory dir = new Directory(getContext(), ROOT_URI); dir.setModifiable(true); dir.setDeeplyAccessible(true); return dir; } }; return application; And the client which is uploading a file looks: Client client = new Client(Protocol.HTTP); Representation rep = new FileRepresentation(fileOpt.getValue(), MediaType.ALL); Response response = client.put(reference, rep); Any suggestions? Thx, christian -- Christian Haintz Student of Software Development and Business Management Graz, University of Technology -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1279726 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1281460 -- Christian Haintz Student of Software Development and Business Management Graz, University of Technology -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1288592
Upload to Directory
Hi, I simply wanna upload a File to the org.restlet.Directory. But all I get is: See Other (303) - The metadata are not consistent with the URI I don't know what that means, or what I can do against it. On the Server Side i do: Application application = new Application() { @Override public Restlet createRoot() { Directory dir = new Directory(getContext(), ROOT_URI); dir.setModifiable(true); dir.setDeeplyAccessible(true); return dir; } }; return application; And the client which is uploading a file looks: Client client = new Client(Protocol.HTTP); Representation rep = new FileRepresentation(fileOpt.getValue(), MediaType.ALL); Response response = client.put(reference, rep); Any suggestions? Thx, christian -- Christian Haintz Student of Software Development and Business Management Graz, University of Technology -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1279726
Re: Upload to Directory
Hello Christian, there is a mechanism based on the MetadaService [1] that checks the media type of the uploaded file with the one deduced from the URI of the resource. For instance, you put a file with mediatype */* on a URI such as http://www.example.com/fille.txt. Based on the metadata service, the media type of the resource is text/plain. The rule is : if */* is not included into text/plain then, reject the uploaded representation. it avoid to send a binary file when a text file is expected. Having said that, this mechanism could be optional, it isn't. best regards, Thierry Boileau Hi, I simply wanna upload a File to the org.restlet.Directory. But all I get is: See Other (303) - The metadata are not consistent with the URI I don't know what that means, or what I can do against it. On the Server Side i do: Application application = new Application() { @Override public Restlet createRoot() { Directory dir = new Directory(getContext(), ROOT_URI); dir.setModifiable(true); dir.setDeeplyAccessible(true); return dir; } }; return application; And the client which is uploading a file looks: Client client = new Client(Protocol.HTTP); Representation rep = new FileRepresentation(fileOpt.getValue(), MediaType.ALL); Response response = client.put(reference, rep); Any suggestions? Thx, christian -- Christian Haintz Student of Software Development and Business Management Graz, University of Technology -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1279726 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=1281460