使用jfilechooser的问题

时间:2013-03-29 06:23:23

标签: java swing web-applications jfilechooser

我在我的网站项目中使用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");

1 个答案:

答案 0 :(得分:1)

我想在我的网络应用程序中做同样的事情,并尝试使用JFileChooser,也遇到了问题。然后我用“内容 - 处置”解决了这个问题。工作原理:用户点击我的应用程序中的按钮/链接,控制器从数据库中取出一些内容并将其写入输出流。系统会询问用户是否要打开此文件或将其保存在计算机上。以下是我在我的应用中完成的操作:

@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文件创建内容的方法,但我认为你不需要它。