我的想法是这样,使用struts上传一份CSV文件,然后服务器端读取。然后进行持久化操作。
我在网上搜了好多例子,搜到的多不能使用。。
求一个完整的例子,要求简单,完整,注释多!~

解决方案 »

  1.   

    首先jsp页面
    <html> 
      <head> 
        <title>My JSP 'fileUpload.jsp' starting page</title> 
          
      </head> 
       
      <body> 
         <form action="jdUpload" enctype="multipart/form-data" method="post"> 
            选择文件<input type="file" name="file"> 
            <input type="submit" value="上传"> 
        </form> 
      </body> 
    </html> 
    然后sturts,xml
    <package name="jingdong" namespace="/page/select/platform" extends="base">

    <action name="jdUpload" method="upLoadFile" class="cn.magaseek.mj.select.controller.JdUpload">
    <result name="success">/page/success.html</result>
    </action>


    </package>
    最后代码:public class JdUpload extends ActionSupport {
    private static final int FILE_SIZE=16*1024;
    private File file;//获取上传文件  
    private String fileName;//获取上传文件名称  
    private String fileContentType;//获取上传文件类型  

    public String upLoadFile(){
    System.out.println("xxx");
    String path = ServletActionContext.getServletContext().getRealPath("/file"); 
            
            System.out.println(path); 
            try {

             String name="FA-GE";
         String fromjd="d://fromjd/"+name+".csv";
         String logjd="d://logjd/"+name+".txt";
         String cid_brand_file="d:\\cid_brand.txt";
         long starttime=new Date().getTime();
         long overtime = 0;
        
         //记录所有日志的str
         String str="运行开始\n";
        
         //添加一件商品的完整流程的方法
         AddItemAndOuterid add=new  AddItemAndOuterid();
        
         // 路径,品牌名,cid_预定品牌对照表
         str=str+add.addItem(fileName,file,cid_brand_file);
        
        
         overtime=new Date().getTime();
         str=str+"运行结束,运行时间为:"+(overtime-starttime);
         WriteHelper.writeToTxt(logjd,str);
         System.out.println("运行结束");
         System.out.println("运行结束,运行时间为:"+(overtime-starttime));


    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
            
            return "success"; 
    } public File getFile() {
    return file;
    }
    public void setFile(File file) {
    this.file = file;
    }
    public String getFileName() {
    return fileName;
    }
    public void setFileName(String fileName) {
    this.fileName = fileName;
    }
    public String getFileContentType() {
    return fileContentType;
    }
    public void setFileContentType(String fileContentType) {
    this.fileContentType = fileContentType;
    }
         


    }这里获得的file就是所需要的file文件