我在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");
最佳答案
我想在我的Web应用程序中做同样的事情,还尝试用JFileChooser进行第一次尝试,但同样遇到了问题。然后,我使用“内容-配置”解决了另一种方法。工作原理:用户在我的应用中单击按钮/链接,控制器从数据库中获取内容并将其写入outputstream。询问用户是否要打开此文件或将其保存在计算机上。这是我在应用程序中执行的操作:
@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文件内容的几种方法,但是我认为您不需要。