setLastModified on jsp UploadFile - java

I need help how to set the last modified time on a file uploaded (on jsp).
I need to know the time when the file uploaded. This is my code but eclipse says "The method setLastModified(Date) is undefined for the type UploadFile".
Code:
UploadFile file = (UploadFile) files.get("uploadfile");
fName =file.getFileName();
file.setLastModified(getthetime());
upBean.store(mrequest, "uploadfile");

I think that the method you are trying to use is a method in the java.io.File API. Change
file.setLastModified(getthetime());
to
new File(fName).setLastModified(getthetime());
For what it is worth, I'm surprised that this would be necessary. I'd have thought that a file uploader would automatically set the modified time to the current time. (Or more accurately, that it would do nothing ... and let the OS set it by default.)
What FileUpload class are you using?

Seems like the message by Eclipse is self-explanatory. In the class UploadFile, there is no method called 'setLastModified'. Therefore, the compilation fails.
What is the fully-qualified classname of UploadFile class? Is it something that you wrote or is it from a third-party library?
Once you get a reference to the java.io.File object, use the setLastModified method in that class to set the time.
You might find it interesting to take a look at Apache Commons File Upload library. It is a well known third-party library that is used to handle file upload operations in Java (see http://commons.apache.org/fileupload/).
This link http://www.servletworld.com/servlet-tutorials/servlet-file-upload-example.html is an example of using Commons File Upload.

Related

File not found in jar after deployment

I have program that throws FileNotFoundException on this statement when the jar is deployed :
final var fopFactory = FopFactory.newInstance(new File("src//main//resources//afp//fop.xconf"));
But this works very well locally with intellij.
I looked around a bit and apparently it needs to go through an InputStream but my FopFactory.newInstance is waiting a File object as a parameter and not an InputStream.
Could you tell me how to work around this problem please ?
Thanks in advance.
A File object points to a file. The resources within a JAR file are not files, so you can not create a File object that points to them.
In Spring, one usually would inject a Resource instead, and pass that Resource to the FopFactory. That presumes that the FopFactory accepts a Resource, or can be changed to accept one.
If that isn't the case, calling code can ask the Resource for an InputStream, and pass that to the FopFactory. If the FopFactory is an org.apache.fop.apps.FopFactory, this can be accomplished by writing FapFactory.newInstance(baseURI, resource.getInputStream()).
If the FopFactory doesn't accept an InputStream either, the only option is to read the InputStream, write it to a temporary file, and pass that File to the FopFactory. But that's really convoluted, and there is nearly always a better option.

Where do i put file(.mp3) in order for it to be read?

I am usinf the JavaFx method of playing music files but it isn't working (sound not playing). I feel the problem lies in my files location. Where do I put the .mp3 in my java projects folder for it to be referenced with a simple string as so? Or is there another way to reference it? JavaFX Media takes a String parameter.
String test = "test.mp3";
Media x = new Media(test);
MediaPlayer mediaPlayer = new MediaPlayer(x);
mediaPlayer.play();
If you have read the JavaDoc of Media, then you know that you have to give a http, file or jar URI to the constructor you are using.
Actually from looking at the source code I wonder that you are not getting an IllegalArgumentException.
However, use Class.getResource(...).toURI().toString() to get the String you want to give to the Media constructor.
The combination of which Class object you call this on and what you give to the getResource() method depends on how you layout your files.
If you have your file besides your class, getClass().getResource("file.name") should work.
If you have your file in the root of your classpath, getClass().getResource("/file.name") should do.
You can give any other valid http, file or jar URI to the contructor too of course.

Is it possible to read a shapefile using geotools WITHOUT specifying the url of the file?

I am creating a web application which will allow the upload of shape files for use later on in the program. I want to be able to read an uploaded shapefile into memory and extract some information from it without doing any explicit writing to the disk. The framework I am using (play-framework) automatically writes a temporary file to the disk when a file is uploaded, but it nicely handles the creation and deletion of said file for me. This file does not have any extension, however, so the traditional means of reading a shapefile via Geotools, like this
public void readInShpAndDoStuff(File the_upload){
Map<String, Serializable> map = new HashMap<>();
map.put( "url", the_upload.toURI().toURL() );
DataStore dataStore = DataStoreFinder.getDataStore( map );
}
fails with an exception which states
NAME_OF_TMP_FILE_HERE is not one of the files types that is known to be associated with a shapefile
After looking at the source of Geotools I see that the file type is checked by looking at the file extension, and since this is a tmp file it has none. (Running file FILENAME shows that the OS recognizes this file as a shapefile).
So at long last my question is, is there a way to read in the shapefile without specifying the Url? Some function or constructor which takes a File object as the argument and doesn't rely on a path? Or is it too much trouble and I should just save a copy on the disk? The latter option is not preferable, since this will likely be operating on a VM server at some point and I don't want to deal with file system specific stuff.
Thanks in advance for any help!
I can't see how this is going to work for you, a shapefile (despite it's name) is a group of 3 (or more) files which share a basename and have extensions of .shp, .dbf, .sbx (and usually .prj, .sbn, .fix, .qix etc).
Is there someway to make play write the extensions with the tempfile name?

Xuggle can't open in-memory input

I am working on a program that integrates Hadoop's MapReduce framework with Xuggle. For that, I am implementing a IURLProtocolHandlerFactory class that reads and writes from and to in-memory Hadoop data objects.
You can see the relevant code here:
https://gist.github.com/4191668
The idea is to register each BytesWritable object in the IURLProtocolHandlerFactory class with a UUID so that when I later refer to that name while opening the file it returns a IURLProtocolHandler instance that is attached to that BytesWritable object and I can read and write from and to memory.
The problem is that I get an exception like this:
java.lang.RuntimeException: could not open: byteswritable:d68ce8fa-c56d-4ff5-bade-a4cfb3f666fe
at com.xuggle.mediatool.MediaReader.open(MediaReader.java:637)
(see also under the posted link)
When debugging I see that the objects are correctly found in the factory, what's more, they are even being read from in the protocol handler. If I remove the listeners from/to the output file, the same happens, so the problem is already with the input. Digging deeper in the code of Xuggle I reach the JNI code (which tries to open the file) and I can't get further than this. This apparently returns an error code.
XugglerJNI.IContainer_open__SWIG_0
I would really appreciate some hint where to go next, how should I continue debugging. Maybe my implementation has a flaw, but I can't see it.
I think the problem you are running into is that a lot of the types of inputs/outputs are converted to a native file descriptor in the IContainer JNI code, but the thing you are passing cannot be converted. It may not be possible to create your own IURLProtocolHandler in this way, because it would, after a trip through XuggleIO.map(), just end up calling IContainer again and then into the IContainer JNI code which will probably try to get a native file descriptor and call avio_open().
However, there may be a couple of things that you can open in IContainer which are not files/have no file descriptors, and which would be handled correctly. The things you can open can be seen in the IContainer code, namely java.io.DataOutput and java.io.DataOutputStream (and the corresponding inputs). I recommend making your DataInput/DataOutput implementation which wraps around BytesReadable/BytesWriteable, and opening it in IContainer.
If that doesn't work, then write your inputs to a temp file and read the outputs from a temp file :)
You can copy file to local first and then try open the container:
filePath = split.getPath();
final FileSystem fileSystem = filePath.getFileSystem(job);
Path localFile = new Path(filePath.getName());
fileSystem.createNewFile(localFile);
fileSystem.copyToLocalFile(filePath, localFile);
int result = container.open(filePath.getName(), IContainer.Type.READ, null);
This code works for me in the RecordReader class.
In your case you may copy the file to local first and then try to create the MediaReader

jface.preference.FileFieldEditor can't specify a new file

I'm setting up a series of preferences in my Eclipse (3.5.2) application and I'm having a problem with the FileFieldEditor. I want to allow the user to specify a log file to print output to. Often, this will be a new file. But when I use the file select dialog with FileFieldEditor, it complains that the file doesn't exists ("Value must be an existing file"). Is there a way, without extending the FileFieldEditor class, to suppress this error and have Java create that file if it doesn't exist? Thanks!
When I look the source code of org.eclipse.jface.preference.FileFieldEditor, the only solution would be to extend it and write your own version of a FileFieldEditor, with:
an overwritten changePressed() method in order to keep the file path even if the file does not exists
an overwritten checkState() method in order to avoid that error message.
So I do not see a way to avoid that FileFieldEditor extension here.

Categories