2016-03-29 24 views
0

をjarファイル私は自分のjsp.The JSPコードでダウンロード]ボタンをクリックすると上の.csvファイルをダウンロードしようとしています以下のようなものです......ダウンロードCSVファイルには、

<form:form method="POST" id="poCSVForm" 
     action="downloadPoCsv" commandName="poCSVcmd" modelAttribute="poCSVcmd"> 

     <div class="content"> 
      <fieldset> 
       <legend>Page Name</legend> 
       <div> 
        <div class="contentpane"> 
         <table> 
          <tr> 
            <td><button type="submit" value="Download" id="downId" class="dwnldbtn">Download</button> 
            <button type="button" class="exit smallbtn" value="Exit">Exit</button></td> 
          </tr> 

         </table> 
        </div> 
       </div> 
      </fieldset> 
     </div> 
    </form:form> 

その後私は、ダウンロードボタンをクリックしていたとき、私のコントローラのコードは今、csvファイルには、変数をフルパスで、すなわち、特定の場所で発生している。このように......

@RequestMapping(value = "/downloadPoCsv", method = RequestMethod.POST) 
public void doPoCsvDownload(
     @ModelAttribute("poCSVcmd") PurchaseOrderCSVBean poCsvBean, 
     Map<String, Object> model, HttpServletRequest req,HttpServletResponse response) throws IOException { 
    CSVWriter writer = null; 
    String filepath = null; 
    try { 

     HttpSession session = req.getSession(); 
     Session hsession = (Session) session 
       .getAttribute(MOLConstants.HIBERNATE_SESSION_KEY); 
     filepath = "purchaseOrder" + new Date().getTime() + ".csv"; 

     ServletContext context = req.getServletContext(); 
     String realPath = context.getRealPath(""); 
     System.out.println("appPath = " + realPath); 
     // construct the complete absolute path of the file 
     String fullPath = realPath + "\\stuff\\" + filepath; 

     System.out.println("fullPath = " + fullPath); 

     File downloadFile = new File(realPath); 
     try { 
      if (!downloadFile.exists()) { 
       if (downloadFile.mkdir()) { 
       } else { 
        throw new RuntimeException("Could not create directory"); 
       } 
      } 
     } catch (Exception e) { 
      throw new RuntimeException(); 
     }   

     String mimeType = context.getMimeType(fullPath); // get MIME type of the file 
     if (mimeType == null) { 

     mimeType = "application/octet-stream"; // set to binary type if MIME mapping not found 
     } 
     System.out.println("MIME type: " + mimeType); 

     // set content attributes for the response 
     response.setContentType(mimeType); 
     response.setContentLength((int) downloadFile.length()); 

     // set headers for the response 
     String headerKey = "Content-Disposition"; 
     String headerValue = String.format("attachment; filename=\"%s\"",downloadFile.getName()); 
     response.setHeader(headerKey, headerValue); 

     List<PoCsvUploadView> csvDataList = poService.getPoCsvData(poCsvBean.getExp_ind(),poCsvBean.getStdt(),poCsvBean.getEnddt()); 

     FileWriter fwriter = new FileWriter(fullPath); 
     writer = new CSVWriter(fwriter, ',', CSVWriter.NO_QUOTE_CHARACTER); 

     String[] header = new String[31]; 

     header[0] = "COMPANY_CD"; 
     ....... 
     header[30] = "VENDOR_TYPE"; 

     List<String[]> li = new ArrayList<String[]>(); 
     li.add(header); 

     for (PoCsvUploadView group : csvDataList) 
     { 
      String[] arr = new String[31]; 

      arr[0] = group.getCompany_cd(); 
      ..... 
      arr[30] = group.getVendor_type(); 
      li.add(arr); 

     } 
     writer.writeAll(li); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     logger.error("Exception >> ", e); 
     throw new CustomGenericException(
       "Error occured while loading report!!"); 
    }finally{ 
     writer.flush(); 
     writer.close(); 
    } 
} 

されます。しかし、そのファイルはブラウザからダウンロードするのではなく、downloadPoCsvという名前のファイルをダウンロードしています(コントローラメソッドの@RequestMappingとまったく同じです)。これは望ましくありません。皆さんはこれについていくつか助けてくれますか?私はOpenCsv jarを使用しています。

答えて

0

ここでは、ファイルをダウンロードしようとしていて、別の名前のファイルをダウンロードしているという問題の説明があるため、問題はSpring MVCではなくopenCSVです。

CodeJavaはpretty good example of a Spring MVC downloadです。試してみてください。

関連する問題