Open file from network drive - java

I'm trying to create hyperlinks to open files from network drive G:
This is part of my testing servlet:
#WebServlet(name="fileHandler", urlPatterns={"/fileHandler/*"})
public class FileServlet extends HttpServlet
{
private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
...
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String requestedFile = request.getPathInfo();
...
File file = new File("G:/test_dir", URLDecoder.decode(requestedFile, "UTF-8")); // cesta se nacita v kazdem doGet
String contentType = getServletContext().getMimeType(file.getName());
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
try
{
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0)
{
output.write(buffer, 0, length);
}
}
finally
{
close(output);
close(input);
}
}
}
My HTML component:
TEST FILE
Network drive is mapped on application server as G:\
Everything is working fine on my localhost application server. I'm able to open file from local drive C: and even from the same network drive G:.
When I start the JSF application on real server, I'm able to open files from local drive only. Not from G: drive.
I've tried to simple JAVA application (to find if java instance has an access to network drive) and it works on both (server and dev.PC):
public class Test
{
static void main(String[] args)
{
String path = "G:/test_dir/test.txt";
File file = new File(path);
System.err.println(file.exists() ? "OK" : "NOK");
}
}
I've tried different URI schemes:
G:/test_dir
G:\\test_dir
And following doesn't work at all:
file://server/g:/test_dir
///server/g:/test_dir
\\\\server\\g\\test_dir ---> in fact, this should work
Where should be a difference between my develop PC and application server?
SOLUTION:
I've found that links to network drive doesn't work in standalone Tomcat, but works in Eclipse + Tomcat, so I have to use complete URI:
Case Eclipse + Tomcat: Path G:/test_dir/test.txt works
Case Standalone Tomcat: Path \\\\server\\g\\test_dir\\test.txt works

If you can debug your server and look at the logs, try this:
String requestedFile = request.getPathInfo();
log.debug('requestedFile='+requestedFile);
String decodedFilename = URLDecoder.decode(requestedFile, "UTF-8");
log.debug('decodedFilename='+decodedFilename);
File dir = new File("G:/test_dir");
log.debug('File g:/test_dir is a dir:'+dir.isDirectory());
File file = new File(dir, decodedFilename);
log.debug('requested file = '+file.getAbsolutePath());
log.debug('file exists = '+file.isFile());
If you do not have a logging framework set up, you can use System.out.println() instead of log.debug(), but that's not recommended for production use.
This won't solve your problem, but you'll be able to see what's going on.

I've found that links to network drive doesn't work in standalone Tomcat, but works in Eclipse + Tomcat, so I have to use complete URI:
Case Eclipse + Tomcat: Path G:/test_dir/test.txt works
Case Standalone Tomcat: Path \\\\server\\g\\test_dir\\test.txt works

Related

SmartGWT ~ DSRequest download file

I am writing a server side class in smartGWT that fetches a record from a table and downloads a binary blob as a file from the database. The goal is to get the binary file attachment to download to local computer.
I was told that I could use DSRequest.getUploadedFile(attachment) to download the file but looking into it, it looks as though this method does not get an uploaded file from the database to download but gets a file to upload to the database.
Is there a way that I can get a file from the database and return it to the user as a download in server side code? We know we can do this in client side code but we would like to be able to do it on the server.
Here is how I am getting the record that contains the file I want to send to the user.
DSRequest dsReq = new DSRequest("table", "fetch");
dsReq.setCriteria("criteria", processFlowData.getVariableMap().get("critera"));
DataSource ds = dsReq.getDataSource();
DSResponse dsResp = ds.execute(dsReq);
if (dsResp.getStatus() < 0) {
//Handle Errors
} else {
if (!dsResp.getRecord().isEmpty()) {
//Download File Here
}
}
I am using Eclipse Kepler, SmartGWT, Java EE.
you could do something like this:
public static void downloadFile(DSRequest dsRequest, RPCManager rpcManager, HttpServletResponse servletResponse) throws Exception {
rpcManager.doCustomResponse();
ServletOutputStream outputStream = servletResponse.getOutputStream();
servletResponse.addHeader("Content-disposition", "attachment; filename=test.pdf");
servletResponse.setContentType("application/x-www-form-urlencoded");
OutputStream responseOutputStream = servletResponse.getOutputStream();
// write file to responseOutputStream
responseOutputStream.flush();
responseOutputStream.close();
}

Intelligently serving jar files from a web server

I am writing a simple (generic) wrapper Java class that will execute on various computers separate from a deployed web server. I want to download the latest version of a jar file that is the application from that associated Web Server (currently Jetty 8).
I have code like this:
// Get the jar URL which contains the application
URL jarFileURL = new URL("jar:http://localhost:8081/myapplication.jar!/");
JarURLConnection jcl = (JarURLConnection) jarFileURL.openConnection();
Attributes attr = jcl.getMainAttributes();
String mainClass = (attr != null)
? attr.getValue(Attributes.Name.MAIN_CLASS)
: null;
if (mainClass != null) // launch the program
This works well, except that myapplication.jar is a large jar file (a OneJar jarfile, so a lot is in there). I would like this to be as efficient as possible. The jar file isn't going to change very often.
Can the jar file be saved to disk (I see how to get a JarFile object, but not to save it)?
More importantly, but related to #1, can the jar file be cached somehow?
2.1 can I (easily) request the MD5 of the jar file on the web server and only download it when that has changed?
2.2 If not is there another caching mechanism, maybe request only the Manifest? Version/Build info could be stored there.
If anyone done something similar could you sketch out in as much detail what to do?
UPDATES PER INITIAL RESPONSES
The suggestion is to use an If-Modified-Since header in the request and the openStream method on the URL to get the jar file to save.
Based on this feedback, I have added one critical piece of info and some more focused questions.
The java program I am describing above runs the program downloaded from the jar file referenced. This program will run from around 30 seconds to maybe 5 minutes or so. Then it is done and exits. Some user may run this program multiple times per day (say even up to 100 times), others may run it as infrequently as once every other week. It should still be smart enough to know if it has the most current version of the jar file.
More Focused Questions:
Will the If-Modified-Since header still work in this usage? If so, will I need completely different code to add that? That is, can you show me how to modify the code presented to include that? Same question with regard to saving the jar file - ultimately I am really surprised (frustrated!) that I can get a JarFile object, but have no way to persist it - will I even need the JarURLConnection class?
Bounty Question
I didn't initially realize the precise question I was trying to ask. It is this:
How can I save a jar file from a web server locally in a command-line program that exits and ONLY update that jar file when it has been changed on the server?
Any answer that, via code examples, shows how that may be done will be awarded the bounty.
Yes, the file can be saved to the disk, you can get the input stream using the method openStream() in URL class.
As per the comment mentioned by #fge there is a way to detect whether the file is modified.
Sample Code:
private void launch() throws IOException {
// Get the jar URL which contains the application
String jarName = "myapplication.jar";
String strUrl = "jar:http://localhost:8081/" + jarName + "!/";
Path cacheDir = Paths.get("cache");
Files.createDirectories(cacheDir);
Path fetchUrl = fetchUrl(cacheDir, jarName, strUrl);
JarURLConnection jcl = (JarURLConnection) fetchUrl.toUri().toURL().openConnection();
Attributes attr = jcl.getMainAttributes();
String mainClass = (attr != null) ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;
if (mainClass != null) {
// launch the program
}
}
private Path fetchUrl(Path cacheDir, String title, String strUrl) throws IOException {
Path cacheFile = cacheDir.resolve(title);
Path cacheFileDate = cacheDir.resolve(title + "_date");
URL url = new URL(strUrl);
URLConnection connection = url.openConnection();
if (Files.exists(cacheFile) && Files.exists(cacheFileDate)) {
String dateValue = Files.readAllLines(cacheFileDate).get(0);
connection.addRequestProperty("If-Modified-Since", dateValue);
String httpStatus = connection.getHeaderField(0);
if (httpStatus.indexOf(" 304 ") == -1) { // assuming that we get status 200 here instead
writeFiles(connection, cacheFile, cacheFileDate);
} else { // else not modified, so do not do anything, we return the cache file
System.out.println("Using cached file");
}
} else {
writeFiles(connection, cacheFile, cacheFileDate);
}
return cacheFile;
}
private void writeFiles(URLConnection connection, Path cacheFile, Path cacheFileDate) throws IOException {
System.out.println("Creating cache entry");
try (InputStream inputStream = connection.getInputStream()) {
Files.copy(inputStream, cacheFile, StandardCopyOption.REPLACE_EXISTING);
}
String lastModified = connection.getHeaderField("Last-Modified");
Files.write(cacheFileDate, lastModified.getBytes());
System.out.println(connection.getHeaderFields());
}
How can I save a jar file from a web server locally in a command-line program that exits and ONLY update that jar file when it has been changed on the server?
With JWS. It has an API so you can control it from your existing code. It already has versioning and caching, and comes with a JAR-serving servlet.
I have assumed that a .md5 file will be available both locally and at the web server. Same logic will apply if you wanted this to be a version control file.
The urls given in the following code need to updated according to your web server location and app context. Here is how your command line code would go
public class Main {
public static void main(String[] args) {
String jarPath = "/Users/nrj/Downloads/local/";
String jarfile = "apache-storm-0.9.3.tar.gz";
String md5File = jarfile + ".md5";
try {
// Update the URL to your real server location and application
// context
URL url = new URL(
"http://localhost:8090/JarServer/myjar?hash=md5&file="
+ URLEncoder.encode(jarfile, "UTF-8"));
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream()));
// get the md5 value from server
String servermd5 = in.readLine();
in.close();
// Read the local md5 file
in = new BufferedReader(new FileReader(jarPath + md5File));
String localmd5 = in.readLine();
in.close();
// compare
if (null != servermd5 && null != localmd5
&& localmd5.trim().equals(servermd5.trim())) {
// TODO - Execute the existing jar
} else {
// Rename the old jar
if (!(new File(jarPath + jarfile).renameTo((new File(jarPath + jarfile
+ String.valueOf(System.currentTimeMillis())))))) {
System.err
.println("Unable to rename old jar file.. please check write access");
}
// Download the new jar
System.out
.println("New jar file found...downloading from server");
url = new URL(
"http://localhost:8090/JarServer/myjar?download=1&file="
+ URLEncoder.encode(jarfile, "UTF-8"));
// Code to download
byte[] buf;
int byteRead = 0;
BufferedOutputStream outStream = new BufferedOutputStream(
new FileOutputStream(jarPath + jarfile));
InputStream is = url.openConnection().getInputStream();
buf = new byte[10240];
while ((byteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, byteRead);
}
outStream.close();
System.out.println("Downloaded Successfully.");
// Now update the md5 file with the new md5
BufferedWriter bw = new BufferedWriter(new FileWriter(md5File));
bw.write(servermd5);
bw.close();
// TODO - Execute the jar, its saved in the same path
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
And just in case you had control over the servlet code as well, this is how the servlet code goes:-
#WebServlet(name = "jarervlet", urlPatterns = { "/myjar" })
public class JarServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// Remember to have a '/' at the end, otherwise code will fail
private static final String PATH_TO_FILES = "/Users/nrj/Downloads/";
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String fileName = req.getParameter("file");
if (null != fileName) {
fileName = URLDecoder.decode(fileName, "UTF-8");
}
String hash = req.getParameter("hash");
if (null != hash && hash.equalsIgnoreCase("md5")) {
resp.getWriter().write(readMd5Hash(fileName));
return;
}
String download = req.getParameter("download");
if (null != download) {
InputStream fis = new FileInputStream(PATH_TO_FILES + fileName);
String mimeType = getServletContext().getMimeType(
PATH_TO_FILES + fileName);
resp.setContentType(mimeType != null ? mimeType
: "application/octet-stream");
resp.setContentLength((int) new File(PATH_TO_FILES + fileName)
.length());
resp.setHeader("Content-Disposition", "attachment; filename=\""
+ fileName + "\"");
ServletOutputStream os = resp.getOutputStream();
byte[] bufferData = new byte[10240];
int read = 0;
while ((read = fis.read(bufferData)) != -1) {
os.write(bufferData, 0, read);
}
os.close();
fis.close();
// Download finished
}
}
private String readMd5Hash(String fileName) {
// We are assuming there is a .md5 file present for each file
// so we read the hash file to return hash
try (BufferedReader br = new BufferedReader(new FileReader(
PATH_TO_FILES + fileName + ".md5"))) {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
I can share experience of solving the same problem in our team. We have several desktop product written in java which are updated regularly.
Couple years ago we had separate update server for every product and following process of update: Client application has an updater wrapper that starts before main logic, and stored in a udpater.jar. Before start, application send request to update server with MD5-hash of application.jar file. Server compares received hash with the one that it has, and send new jar file to updater if hashes are different.
But after many cases, where we confused which build is now in production, and update-server failures we switched to continuous integration practice with TeamCity on top of it.
Every commit done by developer is now tracked by build server. After compilation and test passing build server assigns build number to application and shares app distribution in local network.
Update server now is a simple web server with special structure of static files:
$WEB_SERVER_HOME/
application-builds/
987/
988/
989/
libs/
app.jar
...
changes.txt <- files, that changed from last build
lastversion.txt <- last build number
Updater on client side requests lastversion.txt via HttpClient, retrieves last build number and compares it with client build number stored in manifest.mf.
If update is required, updater harvests all changes made since last update iterating over application-builds/$BUILD_NUM/changes.txt files. After that, updater downloads harvested list of files. There could be jar-files, config files, additional resources etc.
This scheme is seems complex for client updater, but in practice it is very clear and robust.
There is also a bash script that composes structure of files on updater server. Script request TeamCity every minute to get new builds and calculates diff between builds. We also upgrading now this solution to integrate with project management system (Redmine, Youtrack or Jira). The aim is to able product manager to mark build that are approved to be updated.
UPDATE.
I've moved our updater to github, check here: github.com/ancalled/simple-updater
Project contains updater-client on Java, server-side bash scripts (retrieves updates from build-server) and sample application to test updates on it.

Java (NetBeans) Save Image from URL to Local

I'm trying to import a CSV file into a MySQL database. That works perfectly. The problem arises when I try to download an image file from a URL to the server where the servlet is running and save it there.
It works perfectly when I run the servlet on my local machine in NetBeans IDE. It downloads the file, changes it's size finishes. When I try it on the server running in a browser it throws an exception and says it can't find the directory/file.
This is my current code:
try {
saveImage(item[3], idir + prod + ".jpg");
} catch (IOException ioe) {
JOptionPane.showMessageDialog(null, ioe.getMessage());
}
public static void saveImage ( String imageUrl, String destinationFile ) throws IOException {
URL url = new URL(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
}
Like I said, this works perfectly from my local system, only when it's running as a servlet on the server does it not work anymore.
The only other option I am thinking is to run a CLI command from Java and just use WGET to download the file in question. But I would like to steer clear of that if possible.

Can't save file in tomcat webapp running on a linux server

Im currently debugging a webapp, where the user is supposed to upload a file which is then stored temporarily on the server and send via E-Mail to an administrator.
The app runs on a tomcat 8 in a linux server environment. The class from where the error occurs looks like follows:
#Service
public class BugReportBo implements IBugReportBo {
...
#Value("${app.temp.folder}")
private Resource temp;
...
private File byteArrayToFile(final byte[] lob, final String string) throws IOException {
final int locatorStartPos = 6;
String uri = temp.getURI().toString().substring(locatorStartPos);
//uri: var/lib/tomcat/webapps/prototype/res/temp/
//string: temp0.jpg
// convert array of bytes into file
File f = new File(uri + string);
FileOutputStream fileOuputStream = new FileOutputStream(uri + string);
fileOuputStream.write(lob);
fileOuputStream.close();
return f;
}
}
The error is thrown from FileOutputStream fileOuputStream = new FileOutputStream(uri + string);.
The Exception is a FileNotFoundException (message:"var/lib/tomcat/webapps/prototype/res/temp/temp0.jpg (Datei oder Verzeichnis nicht gefunden)").
Everything seems to be ok to me as the folder is where it is supposed to be and the path seems to be fine as well.
Any ideas?
var/lib/tomcat/webapps/prototype/res/temp/temp0.jpg
You forget the first '/', so the path is understand like a relative path, instead of an absolute path.
/var/lib/tomcat/webapps/prototype/res/temp/temp0.jpg

How to download a file from the internet using Java

Hi I am trying to write some code in my program so I can grab a file from the internet but it seems that is not working. Can someone give me some advice please ? Here is my code. In this case I try to download an mp3 file from the last.fm website, my code runs perfectly fine but when I open my downloads directory the file is not there. Any idea ?
public class download {
public static void main(String[] args) throws IOException {
String fileName = "Death Grips - Get Got.mp3";
URL link = new URL("http://www.last.fm/music/+free-music-downloads");
InputStream in = new BufferedInputStream(link.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(response);
fos.close();
System.out.println("Finished");
}
}
Every executing program has a current working directory. Often times, it is the directory where the executable lives (if it was launched in a "normal" way).
Since you didn't specify a path (in fileName), the file will be saved with that name in the current working directory.
If you want the file to be saved in your downloads directory, specify the full path. E.g.
String fileName = "C:\\Users\\YOUR_USERNAME\\Downloads\\Death Grips - Get Got.mp3";
Note how I've escaped the backslashes. Also note that there are methods for joining paths in Java. There is a way to get the current working directory in Java.

Categories

Resources