当你要读取的文件不在当前的计算机上的时候 ,就要涉及远程文件的读取问题了,方法如下:
package action;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URL;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import antlr.preprocessor.Tool;
import entity.Student;
import service.StudentService;
import service.TreeService;
import test.Test1;
import test.TestServlet;
import util.Factory;
import util.Print;
import util.Tools;
public class TestAction extends Action{
private TreeService treeService;
public ActionForward execute(ActionMapping mapping,ActionForm form ,HttpServletRequest request,HttpServletResponse response)throws Exception{
URL url=new URL("http://localhost:8080/009/src.mp3");
InputStream is=url.openStream();
OutputStream os=new FileOutputStream("dest.mp3");
byte bf[]=new byte[1024];
int length=0;
while((length=is.read(bf, 0, 1024))!=-1){
os.write(bf, 0, length);
}
is.close();
os.close();
return null;
}
前提是项目009的根目录下有src.mp3这个文件,其他的配置文件原理类似!!!
}