使用spring传输文件

时间:2014-02-13 05:50:39

标签: java spring

package com.studytrails.tutorials.springremotingrmiclient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;

public class call {
    public String getText()
       {



           ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{"spring-config-client.xml"});       

            StringBuilder builder = new StringBuilder();
             //Resource resource = appContext.getResource("file:D:\\text\\test.txt");
            Resource resource = appContext.getResource("file:D://text//test.txt");

try{


      InputStream is = resource.getInputStream();
   BufferedReader br = new BufferedReader(new InputStreamReader(is));
  File temp=File.createTempFile("output", ".tmp");
  System.out.println("Accepted connection : ");


   String filePath=temp.getAbsolutePath();
  System.out.println(""+filePath);


  String tem=temp.getName();

   String line;
   PrintWriter out = new PrintWriter(new FileWriter(tem));



   while ((line = br.readLine()) != null) {


      out.println(line);


      }

   out.close();
   br.close();
   temp.setReadOnly();


   String[] cmd = {"notepad",tem}; 
   Runtime runtime = Runtime.getRuntime();
   Process proc = runtime.exec(cmd);


   Runtime.getRuntime().exec("cmd /c start \"%programfiles%\\Windows Media Player\\wmplayer.exe\" \"C:\\Users\\Public\\Videos\\Sample Videos\\Wildlife.wmv\""); 
  proc.getInputStream();


   temp.deleteOnExit();


    }catch(IOException e){
        e.printStackTrace();
    }
return builder.toString();

       }

}

在客户端的上述代码中,getText()函数从本地目录中读取文件。我需要从服务器计算机加载文件,我该怎么办。我使用java spring概念进行客户端服务器通信。任何机会在xml bean文件中加载资源。请发布一些使用完整代码

1 个答案:

答案 0 :(得分:0)

如何从URL读取文件?像

这样的东西
URL theFile = new URL("http://www.foobar.com/thefile.txt");

打开一个内流

BufferedReader in = new BufferedReader(new InputStreamReader(theFile.openStream()));

将其内容写入文件。

FileWriter fWriter= new FileWriter(file_name);
BufferedWriter out = new BufferedWriter(fWriter);
out.write(in.readLine());

礼貌。

out.close();
in.close();

Finitio!