2

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");
4

1 回答 1

1

I wanted to do the same in my web app, and also tried frist with JFileChooser, also got the problem as you. Then I solved this other way using "content - disposition". How it works: User click on button/link in my app, controller fetches something from database and write it to outputstream. User is asked if he wants to open this file or save it on computer. Here is how I done that in my app:

@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();
        }
}

I have done similar thing with pdf files:

@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();
        }
    }
}

I didn't copy several methods in which I create content for my pdf file, but I think that you don't need that.

于 2013-03-29T10:31:09.907 回答