2016-05-03 24 views
1

サーバ(.warファイルがデプロイされるドメイン)に.txtファイルを作成してダウンロードしたいダウンロードリンクを介して。私は作成し、私のPCのデスクトップ上のtxtをuser.homeを使用して絶対パスを取得して保存することができます。Spring MVCのWebアプリケーションのweb-infフォルダにtxtファイルを書き込む

私はServletContextを使用してコンテキストパスを取得しています。そのパスにファイルを保存しようとすると、エラーが表示されます。java.io.IOException:指定されたパスが見つかりません。

誰かに私の方法を教えてもらえるようになりました。だから、.warファイルを配備すると、ファイルを書き込んでダウンロードすることができます。

コントローラー:

@Autowired 
ServletContext context; 
@RequestMapping(value = "/calculate") 
public String getDataQuote(ModelMap map, HttpSession session, @ModelAttribute("dataBean") DataBean dataBean) { 
    Calculator calc = new Calculator(); 
    Quote quote = calc.calculate(dataBean); 
    Writer writer = new Writer(); 
    writer.printCloudData(quote, context); 
    map.addAttribute(quote); 
    return "result"; 
} 

ファイルライター:私はこのコントローラを実行し

public void printData(Bean bean, ServletContext context) { 

    try { 
     String path = context.getRealPath("/WebContent/files/"); 
     System.out.println(path); 
     path = path.replace("\\", "/"); 
     File file = new File(path + "/files", "abc.txt"); 
     if (!file.exists()) { 
      file.createNewFile(); 
     } 

     FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
     BufferedWriter bw = new BufferedWriter(fw); 
     bw.write(bean.a); 
     bw.newLine(); 
     bw.newLine(); 
     bw.write(bean.b); 
     bw.close(); 
     } catch (
      IOException e) 
     { 
      e.printStackTrace(); 
     } 
} 

毎回、それがこのエラーを与える:

java.io.IOException: The system cannot find the path specified 
at java.io.WinNTFileSystem.createFileExclusively(Native Method) 
at java.io.File.createNewFile(Unknown Source) 
at com.business.Writer.printQuotationData(Writer.java:32) 
at com.controler.DefaultController.getQuote(DefaultController.java:84) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 

任意の助けをいただければ幸いです。おかげさまで

+0

を。おそらくあなたのアプローチを再考する必要があります。 –

答えて

2

指定したディレクトリがサーバーに存在しませんでした。したがって、例外が発生します。

ファイルライター::次のようにサーバー上のファイルを書くことができますが、WARファイルに書き込むことはできません

public void printData(Bean bean, ServletContext context) { 

try { 
      //if you want to create file outside web-inf directory. 
      //String path = context.getRealPath("/"); 

      //if you want to create file under web-inf directory. 
      String path = context.getRealPath("/WEB-INF"); 

      System.out.println(path); 
      //path = path.replace("\\", "/"); 

      File file = new File(path, "files"); 

      if (!file.exists()) { 
       file.mkdir(); 
      } 

      file = new File(file, "abc.txt"); 
      if (!file.exists()) { 
       file.createNewFile(); 
      } 

     FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
     BufferedWriter bw = new BufferedWriter(fw); 
     bw.write(bean.a); 
     bw.newLine(); 
     bw.newLine(); 
     bw.write(bean.b); 
     bw.close(); 
     } catch (IOException e){ 
      e.printStackTrace(); 
     } 
} 
関連する問題