How can I convert an IPath to a URL. In my case, I want to convert the output location of a Java project to a URL, so that I can load classes from there with the URLClassLoader. My code looks like this:
javaProject.getProject().build(IncrementalProjectBuilder.FULL_BUILD, null);
URL url = new File( javaProject.getOutputLocation().toFile().getAbsolutePath() ).toURI().toURL();
URL[] urls = new URL[] { url };
URLClassLoader classLoader = new URLClassLoader(urls);
Class<?> c = classLoader.loadClass(fully-qualified-class-name);
The problem is that the URL is only a relative one, i.e. its string representation is file:/C:/com.florianingerl.regexfindandreplace.matchevaluators/bin but it should be something like file:/C:/Users/Hermann/runtime-EclipseApplication/com.florianingerl.regexfindandreplace.matchevaluators/bin
The URLClassLoader can't find the classes obviously.
Any help is appreciated.
You can use something like:
IPath path = javaProject.getOutputLocation();
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
URI uri = file.getLocationURI();
URL url = uri.toURL();
Related
A bundle , Get the viewpart icon of the org.eclipse.ui.views extension point
Only one string can be obtained through IConfigurationelement
e.g.( icons/full/obj16/julaunch.png )
But I can't get its bundle, and I can't splice a similar URL
e.g.( platform:/plugin/org.eclipse.unittest.ui/icons/full/obj16/julaunch.png )
What should I do?
If you have an IContributionElement and want to get the resource given by an attribute in the element you can do something like:
String pluginId = element.getContributor().getName();
Bundle bundle = Platform.getBundle(pluginId);
String path = element.getAttribute("attribute id");
URL url = FileLocator.find(bundle, new Path(path), null);
I have following code to save an asset in a servler service
ResourceResolver resourceResolver = this.resolverFactory.getAdministrativeResourceResolver(null);
AssetManager assetMgr = (AssetManager)resourceResolver.adaptTo((Class)AssetManager.class);
String newFile = "/content/dam/travel/" + fileName;
assetMgr.createAsset(newFile, is, "image/jpeg", true);
The following line creates the asset with specified mimetype
assetMgr.createAsset(newFile, is, "image/jpeg", true);
I would like to determine the coming file content type based on stream rather than name.
Thank you,
Sri
I am downloading PDF files from URLs using scala using below code and it is working fine
var out: OutputStream = null;
var in: InputStream = null;
val url = new URL( """http://www.pdf995.com/samples/pdf.pdf""")
val connection = url.openConnection().asInstanceOf[HttpURLConnection]
connection.setRequestMethod("GET")
in = connection.getInputStream
val localfile = "sample2.pdf"
out = new BufferedOutputStream(new FileOutputStream(localfile))
val byteArray = Stream.continually(in.read).takeWhile(-1 !=).map(_.toByte).toArray
out.write(byteArray)
but when I give URLs which does not end with "PDF" for example URL given below
https://www.google.com.pk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=51&ved=0ahUKEwjq19ah8MbKAhXEj44KHeWAB6g4MhAWCBgwAA&url=http%3A%2F%2Fwww.us.fulbrightonline.org%2Fuploads%2Ffiles%2Fapplication_samples%2FForm9B_ETA_Reference_Form-Sample.pdf&usg=AFQjCNGZnon3ygHDJnW12Te8JrBR-o6jyw&sig2=OgSgD4HnUXZ9l_VS0AwGFg&bvm=bv.112454388,d.c2E&cad=rja
it does not generate PDF file properly. While opening that PDF "Not a PDF or corrupted error" comes.
If you read your URL and chop off the hash at the end (all that comes after .pdf, you'll see the link that Google is pointing to embedded in there:
https://www.google.com.pk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=51&ved=0ahUKEwjq19ah8MbKAhXEj44KHeWAB6g4MhAWCBgwAA&url=http%3A%2F%2Fwww.us.fulbrightonline.org%2Fuploads%2Ffiles%2Fapplication_samples%2FForm9B_ETA_Reference_Form-Sample.pdf
Here's the direct link (use this for your project):
http://www.us.fulbrightonline.org/uploads/files/application_samples/Form9B_ETA_Reference_Form-Sample.pdf
I have an application that uploads a file. I need to pass this file into another program but for that I need the file name only. Is there any simple code for that, using only Java or a servlet procedure?
while (files.hasMoreElements())
{
name = (String)files.nextElement();
type = multipartRequest.getContentType(name);
filename = multipartRequest.getFilesystemName(name);
originalFilename = multipartRequest.getOriginalFileName(name);
//extract the file extension - this can be use to reject a
//undesired file extension
extension1 = filename.substring
(filename.length() - 4, filename.length());
extension2 = originalFilename.substring
(originalFilename.length() - 4, originalFilename.length());
//return a File object for the specified uploaded file
File currentFile = multipartRequest.getFile(name);
//InputStream inputStream = new BufferedInputStream
(new FileInputStream(currentFile));
if(currentFile == null) {
out.println("There is no file selected!");
return;
}
There's a method in apache commons-io to get the file's extension. There's also guava Files class, with its getFileExtension method.
Is it possible to retrieve an IDocument from an IFile or IPath? I have tried this:
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(hFilePath);
TextFileDocumentProvider provider = new TextFileDocumentProvider();
IDocument doc = provider.getDocument(file);
but getDocument seems to return null.
Thanks
I was having this same issue and I found a post here that helped. Give this a shot:
IDocumentProvider provider = new TextFileDocumentProvider();
provider.connect(ifile);
document = provider.getDocument(ifile);
That's not the right kind of argument for getDocument(), but you should instead be using the headless FileBuffers APIs, starting from FileBuffers itself, getting the ITextFileBufferManager, and then using it to open a text file buffer and its IDocument: http://help.eclipse.org/juno/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/filebuffers/FileBuffers.html .
Don't forget to disconnect from the file buffer when you're done.
You can do something like this:
IFile file = (IFile) resource;
IPath filePath = file.getLocation();
filePath = FileBuffers.normalizeLocation(filePath);
IDocument document = FileBuffers.getTextFileBufferManager().getTextFileBuffer(filePath , LocationKind.NORMALIZE).getDocument();