I try to get a file in my bundle with:
File file = new File(bundleContext.getBundle().
getResource("image/logo.jpg").toURI());
The result is a IllegalArgumentException with the cause "URI scheme is not "file".
This is logical, but how should i open a file with this URL (bundle://28/image/logo.jpg)?
If i use the regular ClassLoader i get the same result.
EDIT:
My Solution:
URL url = this.getClass().getClassLoader().getResource("image/logo.jpg");
InputStream in = new BufferedInputstream(url.openStream());
You cannot open a file since there might not be a file ... So just get the input stream instead. That works for all URLs/URIs.
Related
I only get one uri from the file_picker when saving. But so that I can display the correct path, I need the absolute path of the file. Unfortunately I haven't found a working solution. The library flutter_absolute_path unfortunately doesn't seem to work with the new Android versions anymore.
Does anyone have any idea how I could get the absolute path of the file.
example:
content://com.android.providers.downloads.documents/document/297
convert to:
/data/user/0/xxx/cache/dummy1.png
Check out the toFilePath() method of the dart:core package to see if it solves your problem.
I thought so too. But unfortunately I get an exception.
test code:
//uri output from file picker (fileInfo.identifier)
var uriPath = "content://com.android.providers.downloads.documents/document/297";
var uri = Uri.parse(uriPath);
print(uri.toFilePath());
exception
-> Unsupported operation: Cannot extract a file path from a content URI
The following code runs without a hitch:
On the other hand, I get an access-denied error with this:
The destination is in my personal folder and I have full control. The directory is not read-only. Anyway, in either of those cases, the first code sample should not run either! I appreciate the help ...
In the second sample, you have two problems:
There are back slashes instead of forward slashes, so some of them may get interpreted as escape sequences.
You completely ignore the first parameter of write and specify what I assume is a folder as destination. You can't open a file stream on a folder, no wonder you get access denied.
This should work:
let write filename (ms:MemoryStream) =
let path = System.IO.Path.Combine( "C:/Users/<whatever>/signal_processor", filename )
use fs = new FileStream( path, FileMode.Create )
ms.WriteTo(fs)
I have a problem with a firefox extension i'm developing.
I need to add an SWF file in the page. If I load it from a remote server, it works fine:
myObj2.setAttribute("data",'http://www.mySite.com/myFile.swf');
myPar1.setAttribute("value",'http://www.mySite.com/myFile.swf');
It works fine but is not accepted for the review.
so I created a resource dir in the manifest:
resource ldvswf swf/
and changed the script into:
myObj2.setAttribute("data",'resource://ldvswf/myFile.swf');
myPar1.setAttribute("value",'resource://ldvswf/myFile.swf');
but it doesn't work. The folder resource://ldvswf is ok as i tested loading an image and I see it.
The reviewer wrote me that for flash file it "requires doing so via a file: URL", but I don't know how to manage, I tested:
'file: resource://ldvswf/myFile.swf'
'file://resource://ldvswf/myFile.swf'
'file://ldvswf/myFile.swf'
'file: ldvswf/myFile.swf'
And nothing works.
Any suggestion for the right path?
Thanks a lot!
Nadia
Update: the editor wrote me:
You need a file URL that points to an actual file. If your extension is unpacked, something like the following should do:
Services.io.newFileURI(Services.io.newURI("resource://ldvswf/myFile.swf", null, null)
.QueryInterface(Ci.nsIFileURL).file)
.spec
But I don't understand how to plce it to replace:
myObj2.setAttribute("data",'http://www.mySite.com/myFile.swf');
myPar1.setAttribute("value",'http://www.mySite.com/myFile.swf');
I made some test like:
var file = Services.io.newFileURI(Services.io.newURI("resource://ldvswf/myFile.swf", null, null).QueryInterface(Ci.nsIFileURL).file).spec ;
myObj2.setAttribute("data",file);
myPar1.setAttribute("value",file);
But I get this error message:
Error: NS_NOINTERFACE: Component returned failure code: 0x80004002 (NS_NOINTERFACE) [nsIFileURL.file]
Have you tried using contentaccessible=yes in your chrome.manifest, like this:
content package_name content/ contentaccessible=yes
I tested it with an image in a local web page and it worked, don't know for SWF:
<img src="chrome://package_name/skin/window.png"/>
Another approach is to get the file URI of the chrome:// (not resource://) file like this:
function chromeToPath (aPath){
if (!aPath || !(/^chrome:/.test(aPath))){
return null; //not a chrome url
}
var Cc = Components.classes, Ci = Components.interfaces;
var spec, url, file;
var ios = Cc['#mozilla.org/network/io-service;1'].getService(Ci["nsIIOService"]);
var uri = ios.newURI(aPath, "UTF-8", null);
var crs = Cc['#mozilla.org/chrome/chrome-registry;1'].getService(Ci["nsIChromeRegistry"]);
var fph = Cc["#mozilla.org/network/protocol;1?name=file"].createInstance(Ci.nsIFileProtocolHandler);
spec = crs.convertChromeURL(uri).spec;
return spec;
}
usage:
chromeToPath('chrome://package_name/skin/window.png')
and it will return the file:// URI of the file.
If you test this, better make sure the extension is unpacked (unzipped) on install:
in install.rdf:
<em:unpack>true</em:unpack>
try breaking the line like this:
var file = Services.io.newURI("resource://ldvswf/myFile.swf", null, null).QueryInterface(Ci.nsIFileURL).file;
alert(file.path)
var file = Services.io.newFileURI(file).spec ;
alert(file)
the first alert() should give a local system path like 'c:\folder\filename...'
the second should give a file:// URI
so what do you get?
make sure you have a line in package.json:
"unpack": true,
at the same level as name, title, author etc. Note that true does not have the quotes.
(https://developer.mozilla.org/en-US/Add-ons/SDK/Tools/package_json)
I am new to Struts and working on File Upload using Struts.
Client:
It is Java Program which hits my Strut app by using apache HttpClient API and provides me
File.
Client as per need sometime gives me .wav file and sometime .zip file and sometime both.
Server:
Struts app which got the request from client app and upload the file.
Here, problem comes as I upload the file, it get uploaded using ".tmp" extension, which I want to get uploaded with the same extension what client has passed.
Or there is any other way by which we can check what is the extension of the file client has sent....?
I am stuck in this problem and not able to go ahead.
Please Find the code attached and tell me what modification I have to do:
Server Code:
MultiPartRequestWrapper multiWrapper=null;
File baseFile=null;
System.out.println("inside do post");
multiWrapper = ((MultiPartRequestWrapper)request);
Enumeration e = multiWrapper.getFileParameterNames();
while (e.hasMoreElements()) {
// get the value of this input tag
String inputValue = (String) e.nextElement();
// Get a File object for the uploaded File
File[] file = multiWrapper.getFiles(inputValue);
// If it's null the upload failed
if (file != null) {
FileInputStream fis=new FileInputStream(file[0]);
System.out.println(file[0].getAbsolutePath());
System.out.println(fis);
int ch;
while((ch=fis.read())!=-1){
System.out.print((char)ch);
}
}
}
System.out.println("III :"+multiWrapper.getParameter("method"));
Client code:
HttpClient client = new HttpClient();
MultipartPostMethod mPost = new MultipartPostMethod(url);
File zipFile = new File("D:\\a.zip");
File wavFile = new File("D:\\b.wav");
mPost.addParameter("recipientFile", zipFile);
mPost.addParameter("promptFile", wavFile);
mPost.addParameter("method", "addCampaign");
statusCode1 = client.executeMethod(mPost);
actually Client is written long back and cant be modified and I want to identify something at server side only to find the extension.
Please help, Thanks.
Struts2 File Uploader interceptor when uploading file pass the content type information to the Action class and one can easily find the file type by comparing contentType with MIME type.
If you want to can create a map with key as content type and file type as its value like
map.Add("image/bmp",".bmp", )
map.Add("image/gif",".gif", )
map.Add("image/jpeg",".jpeg", )
and can easily fetch the type based on the extension provides.Hope this will help you.
I'm designing a jasper report using iReport which takes a parameter and fetches an image from a given URL:
The parameter is a user's screen name in twitter, and the url is it's profile image.
<image>
<reportElement x="4" y="51" width="73" height="64"/>
<imageExpression><![CDATA["https://api.twitter.com/1/users/profile_image?screen_name="+$F{user_screen_name}+"&size=bigger"]]></imageExpression>
</image>
It works great when the image exists. If it doesn't the following exception is thrown:
Error filling print... net.sf.jasperreports.engine.JRException: Error opening input stream from URL :
https://api.twitter.com/1/users/profile_image?screen_name=CPTCurtisHervey&size=bigger
Setting up the file resolver... net.sf.jasperreports.engine.JRRuntimeException:
net.sf.jasperreports.engine.JRException: Error opening input stream from URL :
https://api.twitter.com/1/users/profile_image?screen_name=CPTCurtisHervey&size=bigger
at net.sf.jasperreports.repo.DefaultRepositoryService.getInputStream(DefaultRepositoryService.java:138)
at net.sf.jasperreports.repo.RepositoryUtil.findInputStream(RepositoryUtil.java:186)
at net.sf.jasperreports.repo.RepositoryUtil.getBytes(RepositoryUtil.java:202)
at net.sf.jasperreports.engine.JRImageRenderer.getInstance(JRImageRenderer.java:141)
at net.sf.jasperreports.engine.fill.JRFillImage.evaluateImage(JRFillImage.java:498)
at net.sf.jasperreports.engine.fill.JRFillImage.evaluate(JRFillImage.java:441)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:468)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2037)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:761)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportContent(JRVerticalFiller.java:291)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:133)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:903)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:813)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:58)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:247)
at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:878)
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)
at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997) Caused by:
net.sf.jasperreports.engine.JRException: Error opening input stream from URL :
https://api.twitter.com/1/users/profile_image?screen_name=CPTCurtisHervey&size=bigger
at net.sf.jasperreports.engine.util.JRLoader.getInputStream(JRLoader.java:314)
at net.sf.jasperreports.repo.DefaultRepositoryService.getInputStream(DefaultRepositoryService.java:121)
... 19 more Caused by: java.io.FileNotFoundException:
https://api.twitter.com/1/users/profile_image?screen_name=CPTCurtisHervey&size=bigger
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1029)
at net.sf.jasperreports.engine.util.JRLoader.getInputStream(JRLoader.java:310)... 20 more
Print not filled. Try to use an EmptyDataSource...
How can i handle this situation in the jrxml file?
I would like to simply add a fixed URL address in case it can't find one.
Anyone has any suggestions?
Thank you!
I think you need to add an additional helper class to handle this. You need a static method boolean urlExists(String url) that would allow you to put this in the imageExpression:
MyClass.urlExists($F{image_url}) ? $F{image_url} : $P{fallback_image}
It would be a simple class to write... but clearly there's additional complexity in adding in another .jar file. Without that method, I can't see any way to do the processing in the .jrxml.
in jrxml file,
to show image from url resource, use image expression URL()
like below, for example $F{ADPN_NO} is parameter from report
<imageExpression><![CDATA[new URL("http://anyserver:7001/images/"+$F{ADPN_NO}+".jpg")]]></imageExpression>
when onError occurred, it will show blank item, icon, or error that is set on jrxml.
the other way, outside of jrxml file, you can use java map object which contains img urls that is check by URL() or File() Exception ..
In my case I had a report to which I "injected" the image's url and it was causing an error with the certificate (the url I was "injecting" had the IP and it had to be a name for the certificate). After I changed the url it worked.