我是軟件開發領域的新人,剛上大學。做我的第一個大項目。如何從java彈簧控制器GUI上下載CSV文件?
我想下載CSV文件,當用戶選擇項目的開始日期和結束日期,所以該文件應該返回project.csv與項目名稱,日期從..到..
最重要的是,我試圖在點擊「導出」鏈接後從GUI下載文件。我所知道的是我必須製造一個彈簧控制器。我不得不錯過一些部分,因爲它不工作。 我的java類正在將csv文件寫入我的C盤,但它不會執行下載部分。另外,CSV文件應該從數據庫寫入用戶計算機,而不是寫入我的磁盤。
希望你能理解我。讓我知道如果這是明確的。
我的代碼:
ExportController.java
@RestController
@RequestMapping("/config")
public class ExportController {
private String filePath = "C:\\test\\project.csv";
private static final int BUFFER_SIZE = 4096;
@Autowired
private ExportService exportService;
ServletContext context;
@RequestMapping(value = "export/all", method = RequestMethod.POST)
public String list(@RequestParam("startDate")String date, @RequestParam("endDate")String date2, HttpServletResponse response, HttpServletRequest request) throws ServletException, ParseException, IOException {
DateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
Date date_obj = format.parse(date);
Date date2_obj = format.parse(date2);
// get absolute path of the application
ServletContext context = request.getServletContext();
String appPath = context.getRealPath("");
System.out.println("appPath = " + appPath);
// construct the complete absolute path of the file
String fullPath = filePath;
File downloadFile = new File(fullPath);
FileInputStream inputStream = new FileInputStream(downloadFile);
// get MIME type of the file
String mimeType = context.getMimeType(fullPath);
if (mimeType == null) {
// set to binary type if MIME mapping not found
mimeType = "application/octet-stream";
}
System.out.println("MIME type: " + mimeType);
CsvWriterProject.savetofile();
String csv = "Employee FN/LN: Eatrick Hughes Contract type: External, Activity: WAR, Effort date: 2016-02-17, Name: udfuhfd, Entity: BA, Start date: 2016-02-17, End_date: 2016-02-18";
response.setContentType("application/csv");
response.setHeader("Content-Disposition", "attachment; filename=project.csv");
response.setHeader("Pragma", "public");
response.setHeader("Expires", "0");
response.setHeader("Content-Length", String.valueOf(csv.length()));
response.setHeader("Content-type","application/csv");
// response.setContentType(mimeType);
// response.setContentLength((int) downloadFile.length());
// get output stream of the response
OutputStream outStream = response.getOutputStream();
PrintWriter pw = new PrintWriter(outStream);
pw.print(pw);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
// write bytes read from the input stream into the output stream
while ((bytesRead = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outStream.flush();
outStream.close();
return csv;
}
}
這裏是angularJS
$scope.export_all = function(item){
$http.post('config/export/all?startDate='+item.startDate+"&endDate="+item.endDate)
.success(function(response) {
$scope.export = response;
});
};
讓我知道如果你需要更多的信息。
使用POST方法的具體原因是什麼? – VinayVeluri
是的。 「NetworkError:405方法不允許 - http:// localhost:8080/config/export/all?startDate = 29-04-2016&endDate = 08-05-2016」 – AgnesDbk