从RESTful服务器下载PDF文件失败

时间:2014-08-15 02:52:21

标签: android rest get download client-server

我尝试下载文件,但客户端显示警告 java.net.SocketTimoutException 找不到文件。我不知道故障在哪里。

客户代码

public void downloadPdfJpgFromUrl(String DownloadUrl, String fileName) 
{
    int status = 0 ;
    String LOG_TAG = "DownloadManager", strDownloadString="";
    GetMethod method = new GetMethod();
    try {
        HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(5000);
        method.setURI(new URI(DownloadUrl, true));
        URL url = new URL(DownloadUrl);
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();

        status = httpClient.executeMethod(method);
        System.out.println("HTTP status " + method.getStatusCode()
                + " creating con\n\n");

        File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/pdf/");
        if (!myFilesDir.exists()){
            myFilesDir.mkdir();}

            InputStream is = c.getInputStream();
            c.getOutputStream().write( ("file" + fileName).getBytes()); 
            File outputFile = new File(Environment.getExternalStorageDirectory(),s_storagepath+s_nama_file+s_pdfext);
            FileOutputStream fos = new FileOutputStream(outputFile);

            byte[] buffer = new byte[1024];
            int len1 = 0;
            int size = 1 ;
            while ((len1 = is.read(buffer)) != -1) {
                Log.v(LOG_TAG, strDownloadString+size);
                fos.write(buffer, 0, len1);
                size++;
            }
            fos.close();
            is.close();

        } catch(ConnectTimeoutException e){
            Log.e("Timeout Exception: ", e.toString());
        } catch(SocketTimeoutException ste){    
            Log.e("Timeout Exception: ", ste.toString());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace(); 
        }finally { 
            method.releaseConnection(); 
        }
        return;
    }
}

我在客户端使用URLencoder作为fileName:

(path="http://localhost:8080/downloadFile/~~path~folder~in~server"+"/pdf/"+URLEncoder.encode(fileName, "UTF-8")+"/pdf";)

服务器代码:

@Path("/downloadFile/{pathName}/{folderFile}/{fileName}/{typeFile}")
@GET
public String downloadFile(@PathParam("pathName") String pathName, 
        @PathParam("folderFile") String folderFile, @PathParam("fileName") String fileName, 
        @PathParam("typeFile") String typeFile) throws IOException {
    InputStream inStream = null;
    OutputStream outStream = null;
    pathName=pathName.replace("~", "\\");
    fileName=URLDecoder.decode(fileName, "UTF-8");
    try{
        File file =new File(fileName+"."+typeFile); 
        File bfile =new File(pathName+"\\"+folderFile+"\\"+fileName+"."+typeFile); 
        System.out.println(bfile);

        inStream = new FileInputStream(file);
        outStream = new FileOutputStream(bfile);

        byte[] buffer = new byte[1024];

        int length;
        while ((length = inStream.read(buffer)) > 0){
        outStream.write(buffer, 0, length);
        }
    }catch(Exception e){
    }finally{
        outStream.flush();
        outStream.close();
    }
    return null;
}

0 个答案:

没有答案