文件合并,通过指定目录,将具有相同后缀标识(如part)的一组文件合并我用下面的方法,似乎传递路径时有问题
import java.io.*;
import java.lang.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class heservlet extends HttpServlet{

private ServletConfig config;
//初始化Servlet
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}

/*文件合并函数*/
public void unite(String filename,String path,String lastname,HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException, Exception{
response.setContentType("text/html;charset=GBK");

    String [] tt;
    String str="";
    final String filterName=lastname;
   
   
     File inFile = new File(path); //在当前目录下的文件
     File outFile = new File(path,filename);  //取得输出名
     RandomAccessFile outt= new RandomAccessFile(outFile,"rw"); 
   
     //取得符合条件的文件名
     tt = inFile.list(new FilenameFilter(){
      public boolean accept(File dir,String name){
        String rr = new File(name).toString();
       return rr.endsWith(filterName);
      }
     });
    
    
    //打开所有的文件再写入到一个文件里
    for(int i=0;i<tt.length;i++){
     inFile = new File(path,tt[i]);
     RandomAccessFile inn= new RandomAccessFile(inFile,"r");
     int c;
     while((c=inn.read())!=-1)
      outt.write(c);
    }
    
    outt.close();
   }
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();

  request.setCharacterEncoding("GBK");
  String filename=request.getParameter("objectfile");
  String lastname=request.getParameter("rename");
String path="D:\\tom_cat\\test"; //指定路径
out.println(path+"<br>");

try{
unite(filename,path,lastname,request,response);
out.println("合并成功");
}catch(Exception e){
e.printStackTrace(); 
out.println("合并失败");
} }

}

解决方案 »

  1.   

    页面总是报错
    message description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: Servlet execution threw an exception
    root cause java.lang.NoClassDefFoundError: heservlet$1
    heservlet.unite(heservlet.java:34)
    heservlet.doPost(heservlet.java:68)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      

  2.   

    我想知道servlet中究竟怎样传递文件路径和文件名String path="D:\\tom_cat\\test"; //指定路径
    File inFile = new File(path); //在当前目录下的文件
    像这样指定路径,能不能读取这个目录下所有文件还有
    //取得符合条件的文件名
            tt = inFile.list(new FilenameFilter(){
             public boolean accept(File dir,String name){
            String rr = new File(name).toString();
              return rr.endsWith(filterName);
             }
            });
    在dos中运行则没问题,在servlet中好像有问题
      

  3.   

    你想干吗?这样写servlet跑的是服务器"D:\\tom_cat\\test"的文件