Name: nt126004 Date: 04/23/2002
FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
FULL OPERATING SYSTEM VERSION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
When using a URLConnection, if a URL is an ftp type
referencing a .doc, .ppt, or .xls file uc.getContent()
does not complete and the inputstream stops before the
real end of file.
The problem is not so much with the not finding the file types in the default built-in file map, but the fact that uc.getContent() for these type hangs only when using ftp. This I believe is due to the fact that getContentTypeFor(filename); return null for these types and not "unknown/content" or "application/octet-stream" like it should.
Please reference source code included.
This bug can be reproduced always.
ERROR MESSAGES THAT APPEAR:
here is the stack trace when the program has hung:
^\Full thread dump Java HotSpot(TM) Client VM (1.4.0-b92 mixed mode):
"Signal Dispatcher" daemon prio=10 tid=0xa66f8 nid=0x9 waiting on monitor [0..0]
"Finalizer" daemon prio=8 tid=0xa14c0 nid=0x6 waiting on monitor [fa381000..fa3819c8]
at java.lang.Object.wait(Native Method)
- waiting on <f2000490> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:111)
- locked <f2000490> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:127)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:159)
"Reference Handler" daemon prio=10 tid=0xa0ac8 nid=0x5 waiting on monitor [fdf81000..fdf819c8]
at java.lang.Object.wait(Native Method)
- waiting on <f2000380> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:426)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:113)
- locked <f2000380> (a java.lang.ref.Reference$Lock)
"main" prio=5 tid=0x2b2d0 nid=0x1 runnable [ffbed000..ffbee140]
at java.net.URLConnection.checkfpx(URLConnection.java:1403)
at java.net.URLConnection.guessContentTypeFromStream(URLConnection.java:1305)
at sun.net.www.URLConnection.getContentType(URLConnection.java:112)
at java.net.URLConnection.getContentHandler(URLConnection.java:1056)
- locked <f20422c0> (a sun.net.www.protocol.ftp.FtpURLConnection)
at java.net.URLConnection.getContent(URLConnection.java:583)
at FileLoad.loadFile(FileLoad.java:41)
at FileLoad.main(FileLoad.java:74)
"VM Thread" prio=5 tid=0x9f750 nid=0x4 runnable
"VM Periodic Task Thread" prio=10 tid=0xa5438 nid=0x7 waiting on monitor
"Suspend Checker Thread" prio=10 tid=0xa5d98 nid=0x8 runnable
---------- BEGIN SOURCE ----------
import java.io.*;
import java.util.*;
import java.net.*;
class FileLoad {
static int sz=1000;
public void loadFile(String filename) {
try {
// This block below fixes the problem. Seems as though FileNameMap
// for URLConnection is returning null for .doc, .ppt, .xls
/**
final FileNameMap map=URLConnection.getFileNameMap();
URLConnection.setFileNameMap(new FileNameMap(){
public String getContentTypeFor(String filename){
final String r=map.getContentTypeFor(filename);
System.err.println("map "+filename+"=>"+r);
return r!=null?r:"application/octet-stream";}});
**/
URL u = new URL(filename);
System.err.println("URL: " + u);
URLConnection uc = u.openConnection();
System.err.println("uc="+uc+"("+uc.getClass()+")");
System.err.println(uc.getContentEncoding());
// This HANGS if it is a ftp url that points to a powerpoint,
// word, or excel document but
// works on all other ftp urls with the Solaris ftp daemon, and 2
// windows xp ftp daemons
// if commented out, this example gets the whole file if it is not
// a powerpoint, word or excel
// document and if it is gets only a partial file. This is
// directly related to FileNameMap
// returning null for these file extensions.
System.err.println(uc.getContent());
System.err.println("Content Length of File: " + uc.getContentLength
());
InputStream is = uc.getInputStream();
int size = 0;
int len = 0;
byte[] b = new byte[sz];
while ((len = is.read(b)) > 0) {
System.err.println("+"+len);
size+=len;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args []) {
// The below files were used for testing on my local area network.
//String filename = "file://c:/dlite.doc"; // works just fine all cases
//String filename = "ftp://test1:test@oneida/%2Ftmp/dlite.doc"; // does not work fine, ftp url to a .doc file
String filename = "ftp://test1:test@oneida/%2Ftmp/test.zip"; // works just fine all cases
FileLoad fload = new FileLoad();
fload.loadFile(filename);
}
}
---------- END SOURCE ----------
CUSTOMER WORKAROUND :
final FileNameMap map=URLConnection.getFileNameMap();
URLConnection.setFileNameMap(new FileNameMap(){
public String getContentTypeFor(String filename){
final String r=map.getContentTypeFor(filename);
System.err.println("map "+filename+"=>"+r);
return r!=null?r:"application/octet-stream";}});
(Review ID: 145519)
======================================================================