gpt4 book ai didi

java - 使用 jfilechooser 的问题

转载 作者:行者123 更新时间:2023-11-30 11:34:22 24 4
gpt4 key购买 nike

我在我的 web 项目中使用 JFileChooser 以便客户端可以从服务器下载某些文件,但是在测试期间,每当我单击调用 jfilechooser 的按钮时它在服务器系统上弹出,而不是在客户端系统上弹出,为什么会这样,我该如何解决,下面是我使用的代码。

JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("Enter the path to save .pdf file");
jfc.setFileView(new FileView() {
jfc.setMultiSelectionEnabled(false);
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int status = jfc.showDialog(null, "OK");

最佳答案

我想在我的网络应用程序中做同样的事情,并且首先尝试使用 JFileChooser,也遇到了你的问题。然后我用“内容 - 处置”解决了这个问题。它是如何工作的:用户点击我的应用程序中的按钮/链接, Controller 从数据库中获取一些东西并将其写入输出流。询问用户是否要打开此文件或将其保存在计算机上。以下是我在我的应用程序中的做法:

@Controller
public class ExportPhonebook extends HttpServlet {

private static final long serialVersionUID = 1L;

@Autowired
private PhoneBookService phoneBookSer;

private void setResponseHeaderTXT(HttpServletResponse response) {
response.setContentType("text/plain; charset=UTF-8");
response.setHeader("content-disposition", "attachment; filename=imenik.txt" );
}

@RequestMapping(value = "/exportPhonebook.html", method = RequestMethod.POST)
public void exportPhonebook(Model model, HttpServletResponse response, HttpServletRequest request) {

List<User> listOfAllUsers = phoneBookSer.fetchAllUsers();
String imenik = "";

for (User user : listOfAllUsers) {
imenik = imenik + user.getPrezime() + " " + user.getIme() + ", Organizacijska jedinica: " + user.getOrganizacijskaJedinica() +
", Telefon: " + user.getTelefon() + ", Faks: " + user.getTelefaks() + ", Email: " + user.getEmail() +
", Lokacija: " + user.getLokacija() + ", Kat: " + user.getKat() + ", Soba: " + user.getBrojSobe() + ";\r\n" ;
}


try {
setResponseHeaderTXT(response);
OutputStream outputStream = response.getOutputStream();
outputStream.write(imenik.getBytes(Charset.forName("UTF-8")));
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

我对 pdf 文件做过类似的事情:

@Controller
public class ExportPhonebook extends HttpServlet {

private static final long serialVersionUID = 1L;

@Autowired
private PhoneBookService phoneBookSer;

private void setResponseHeaderPDF(HttpServletResponse response) {
response.setContentType("application/pdf");
response.setHeader("content-disposition", "attachment; filename=imenik.pdf" );
}

@RequestMapping(value = "/exportPhonebook.html", method = RequestMethod.POST)
public void exportPhonebook(Model model, HttpServletResponse response, HttpServletRequest request) {

try {
setResponseHeaderPDF(response);
Document document = new Document();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter pdfWriter = null;
pdfWriter = PdfWriter.getInstance(document, baosPDF);
PageNumbersEventHelper events = new PageNumbersEventHelper();
pdfWriter.setPageEvent(events);

document.open();
addMetaData(document);
addTitlePage(document);
ServletContext servletContext = request.getSession().getServletContext();

String relativeWebPath = "img/image.png";
String absoluteDiskPath = servletContext.getRealPath(relativeWebPath);
Image image = Image.getInstance(absoluteDiskPath);
image.setAbsolutePosition(450, 700);
document.add(image);
addContent(document);
document.close();
pdfWriter.close();
OutputStream os = response.getOutputStream();
baosPDF.writeTo(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

我没有复制我为 pdf 文件创建内容的几种方法,但我认为您不需要那个。

关于java - 使用 jfilechooser 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15698408/

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