如下:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
@Controller
public class TestController {
@RequestMapping(value = "/test")
public void erst(HttpServletRequest request, HttpServletResponse response) throws IOException {
String reportName = "export.csv";
response.setContentType("application/octet-stream; charset=UTF-8");
response.setHeader("Content-Encoding", "UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + reportName);
OutputStream outputStream = null;
outputStream = response.getOutputStream();
outputStream.write(0xEF); // 1st byte of BOM
outputStream.write(0xBB);
outputStream.write(0xBF); // last byte of BOM
// now get a PrintWriter to stream the chars.
PrintWriter os = new PrintWriter(new OutputStreamWriter(outputStream,"UTF-8"));
StringBuffer sb = new StringBuffer();
sb.append("Sender Number,Campaign Title,Requested Time手动阀手动阀,Sent Time,Size,Status,Recipients");
sb.append("\n");sb.append("85245699654" + ",");
os.print(sb);
os.flush();
os.close();
}
}