gpt4 book ai didi

java - 如何在旅途中从 Java Spring Controller 的 GUI 下载 CSV 文件?

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:53 25 4
gpt4 key购买 nike

我是软件开发领域的新手,刚刚大学毕业。做我的第一个大项目。

当用户选择项目的开始日期和结束日期时,我尝试下载 CSV 文件,因此该文件应该返回包含项目名称、日期从 .. 到 .. 的 project.csv

最重要的是,我在单击“导出”链接后尝试从 GUI 下载文件。我所知道的是我必须制作一个 Spring Controller 。我必须遗漏某些部分,因为它不起作用。我的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;

});
};

如果您需要更多信息,请告诉我。

最佳答案

您可以使用 HttpServletResponse (javax.servlet.http.HttpServletResponse)

这里是示例代码:

package com.export.test;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/exportTest")
public class ExportControllerTest {
@RequestMapping(value = "/export", method = RequestMethod.GET)
public void exportStream(HttpServletResponse response) {
try {
String responseTosend = "Testing export for rest cliet";
response.getOutputStream()
.write((responseTosend).getBytes("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
}

根据您的要求进行修改。查看文档以获取更多信息 https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html

关于java - 如何在旅途中从 Java Spring Controller 的 GUI 下载 CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35745707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com