Java 空指针异常jnlp.FileSaveService
我正在尝试使用jnlp.FileSaveService来保存在java web app的contoroller中生成的“saveAs”.txt文件。我在这里找到了一些例子 我不完全理解这是如何工作的,我在这一行得到null poniter异常:Java 空指针异常jnlp.FileSaveService,java,tomcat,spring-mvc,nullpointerexception,jnlp,Java,Tomcat,Spring Mvc,Nullpointerexception,Jnlp,我正在尝试使用jnlp.FileSaveService来保存在java web app的contoroller中生成的“saveAs”.txt文件。我在这里找到了一些例子 我不完全理解这是如何工作的,我在这一行得到null poniter异常: fc = fss.saveFileDialog(null, null, new ByteArrayInputStream(txt.getBytes()), null); 我是否向方法saveFileDialog传递了错误信息,还是传递了其他信息?我将感
fc = fss.saveFileDialog(null, null, new ByteArrayInputStream(txt.getBytes()), null);
我是否向方法saveFileDialog传递了错误信息,还是传递了其他信息?我将感谢任何帮助
static private FileOpenService fos = null;
static private FileSaveService fss = null;
static private FileContents fc = null;
private static synchronized void initialize() {
try {
fos = (FileOpenService)ServiceManager.lookup("javax.jnlp.FileOpenService");
fss = (FileSaveService)ServiceManager.lookup("javax.jnlp.FileSaveService");
} catch (UnavailableServiceException e) {
}
}
public static void save(String txt) {
initialize();
try {
// Show save dialog if no name is already given
if (fc == null) {
fc = fss.saveFileDialog(null, null, new ByteArrayInputStream(txt.getBytes()), null);
// file saved, done
return;
}
// use this only when filename is known
if (fc != null) {
writeToFile(txt, fc);
}
} catch (IOException ioe) {ioe.printStackTrace(System.out);
}
}
public static void saveAs(String txt) {
initialize();
try {
if (fc == null) {
// If not already saved. Save-as is like save
save(txt);
} else {
fc = fss.saveAsFileDialog(null, null, fc);
save(txt);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
}
private static void writeToFile(String txt, FileContents fc) throws IOException {
int sizeNeeded = txt.length() * 2;
if (sizeNeeded > fc.getMaxLength()) {
fc.setMaxLength(sizeNeeded);
}
BufferedWriter os = new BufferedWriter(new OutputStreamWriter(fc.getOutputStream(true)));
os.write(txt);
os.close();
}
@RequestMapping(value = "/exportPhonebook.html", method = RequestMethod.POST)
public String exportPhonebook(Model model) {
List<User> listOfAllUsers = phoneBookSer.fetchAllUsers();
String phonebook = "";
for (User user : listOfAllUsers) {
phonebook = phonebook + user.getSurname() + " " + user.getName() +
", Phone Number: " + user.getPhoneNumber() + ";\r\n" ;
}
saveAs(phonebook);
}
我采取了不同的方法来解决我的问题。我使用
content-disposition
保存控制器中生成的.txt文件。你可以在下面看到我的代码
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) {
List<User> listOfAllUsers = phoneBookSer.fetchAllUsers();
String phonebook = "";
for (User user : listOfAllUsers) {
phonebook = phonebook + user.getSurname() + " " + user.getName() + ", +
", Phone number: " + user.getPhoneNumber() + ";\r\n" ;
}
try {
setResponseHeaderTXT(response);
OutputStream outputStream = response.getOutputStream();
outputStream.write(phonebook.getBytes(Charset.forName("UTF-8")));
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void setresponseheaderText(HttpServletResponse){
response.setContentType(“text/plain;charset=UTF-8”);
setHeader(“内容处置”、“附件;文件名=imenik.txt”);
}
@RequestMapping(value=“/exportPhonebook.html”,method=RequestMethod.POST)
public void exportPhonebook(模型,HttpServletResponse){
List listOfAllUsers=phoneBookSer.fetchAllUsers();
字符串phonebook=“”;
for(用户:所有用户列表){
phonebook=phonebook+user.getName()+“”+user.getName()+“”,+
,电话号码:“+user.getPhoneNumber()+”;\r\n”;
}
试一试{
SetResponseHeaderText(响应);
OutputStream OutputStream=response.getOutputStream();
outputStream.write(phonebook.getBytes(Charset.forName(“UTF-8”)));
outputStream.flush();
outputStream.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
我用IText将电话簿导出为pdf格式时也做了类似的事情
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, @RequestParam("buttonSpremiImenik") String buttonSpremiImenik, HttpServletResponse response, HttpServletRequest request) {
try {
setResponseHeaderPDF(response);
Document document = new Document();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter pdfWriter = null;
pdfWriter = PdfWriter.getInstance(document, baosPDF);
document.open();
addMetaData(document);
addTitlePage(document);
addContent(document);
document.close();
pdfWriter.close();
OutputStream os = response.getOutputStream();
baosPDF.writeTo(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);
private static Font tableHeaderFont = new Font(Font.FontFamily.TIMES_ROMAN, 10,Font.BOLD);
public static String[][] FONTS = {{"c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H}};
private static void addMetaData(Document document) {
document.addTitle("Phonebook");
document.addSubject("phonebook");
document.addKeywords("phonebook");
document.addAuthor("John Smith");
document.addCreator("John Smith");
}
private static void addTitlePage(Document document) throws DocumentException {
Paragraph preface = new Paragraph();
addEmptyLine(preface, 1);
preface.add(new Paragraph("Phonebook", catFont));
addEmptyLine(preface, 1);
preface.add(new Paragraph("Generated: " + new Date(), smallBold));
addEmptyLine(preface, 1);
document.add(preface);
}
private void addContent(Document document) throws DocumentException {
Paragraph paragraph = new Paragraph();
addEmptyLine(paragraph, 1);
// Add a table
createTable(paragraph);
// Now add all this to the document
document.add(paragraph);
}
private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
paragraph.add(new Paragraph(" "));
}
}
private void createTable(Paragraph paragraf) throws DocumentException {
PdfPTable table = new PdfPTable(9);
table.setTotalWidth(new float[]{ 58, 50, 95, 60, 60, 135, 60, 42, 30 });
table.setLockedWidth(true);
List<User> listOfAllUsers = phoneBookSer.fetchAllUsers();
PdfPCell c1 = new PdfPCell(new Phrase("Prezime", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Ime", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Organizacijska jedinica", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Telefon", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Telefaks", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("E-mail", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Lokacija", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Kat", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Soba", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
BaseFont bf = null;
try {
bf = BaseFont.createFont(FONTS[0][0], FONTS[0][1], BaseFont.EMBEDDED);
} catch (IOException e) {
e.printStackTrace();
}
Font fontArial = new Font(bf, 9);
//Automatically detects fonts
//FontFactory.registerDirectories();
//Font fontArial9 = FontFactory.getFont("Arial", 9);
for (User user : listOfAllUsers) {
PdfPCell cell1 = new PdfPCell(new Phrase(user.getPrezime(), fontArial));
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase(user.getIme(), fontArial));
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Phrase(user.getOrganizacijskaJedinica(), fontArial));
table.addCell(cell3);
PdfPCell cell4 = new PdfPCell(new Phrase(user.getTelefon(), fontArial));
table.addCell(cell4);
PdfPCell cell5 = new PdfPCell(new Phrase(user.getTelefaks(), fontArial));
table.addCell(cell5);
PdfPCell cell6 = new PdfPCell(new Phrase(user.getEmail(), fontArial));
table.addCell(cell6);
PdfPCell cell7 = new PdfPCell(new Phrase(user.getLokacija(), fontArial));
table.addCell(cell7);
PdfPCell cell8 = new PdfPCell(new Phrase(user.getKat(), fontArial));
table.addCell(cell8);
PdfPCell cell9 = new PdfPCell(new Phrase(user.getBrojSobe(), fontArial));
table.addCell(cell9);
}
paragraf.add(table);
}
private void setResponseHeaderPDF(HttpServletResponse){
response.setContentType(“application/pdf”);
setHeader(“内容处置”、“附件;文件名=imenik.pdf”);
}
@RequestMapping(value=“/exportPhonebook.html”,method=RequestMethod.POST)
public void exportPhonebook(模型,@RequestParam(“buttonspremimenik”)字符串buttonspremimenik,HttpServletResponse,HttpServletRequest){
试一试{
setResponseHeaderPDF(响应);
文档=新文档();
ByteArrayOutputStream=newbytearrayoutputstream();
PdfWriter PdfWriter=null;
pdfWriter=pdfWriter.getInstance(文档,pdf);
document.open();
添加元数据(文件);
添加标题页(文件);
添加内容(文件);
document.close();
pdfWriter.close();
OutputStream os=response.getOutputStream();
pdf.writeTo(os);;
os.flush();
os.close();
}捕获(例外e){
e、 printStackTrace();
}
}
私有静态字体catFont=新字体(Font.FontFamily.TIMES_ROMAN,18,Font.BOLD);
私有静态字体smallBold=新字体(Font.fontf家族.TIMES_-ROMAN,12,Font.BOLD);
private static Font tableHeaderFont=新字体(Font.fontf家族.TIMES_-ROMAN,10,Font.BOLD);
公共静态字符串[][]字体={{“c:/windows/FONTS/arial.ttf”,BaseFont.IDENTITY_H};
私有静态void addMetaData(文档){
文件。addTitle(“电话簿”);
文件。添加主题(“电话簿”);
document.addKeywords(“电话簿”);
文件作者(以下简称“约翰·史密斯”);
文件。addCreator(“约翰·史密斯”);
}
私有静态void addTitlePage(文档文档)引发DocumentException{
段落前言=新段落();
addEmptyLine(前言,1);
序言。添加(新段落(“电话簿”,catFont));
addEmptyLine(前言,1);
序言.添加(新段落(“生成:“+new Date(),smallBold));
addEmptyLine(前言,1);
文件.增加(序言);
}
私有void addContent(文档)引发DocumentException{
段落=新段落();
addEmptyLine(第1款);
//添加一个表
表(第段);
//现在将所有这些添加到文档中
文件.添加(第段);
}
专用静态void addEmptyLine(段落,整数){
for(int i=0;iprivate 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, @RequestParam("buttonSpremiImenik") String buttonSpremiImenik, HttpServletResponse response, HttpServletRequest request) {
try {
setResponseHeaderPDF(response);
Document document = new Document();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter pdfWriter = null;
pdfWriter = PdfWriter.getInstance(document, baosPDF);
document.open();
addMetaData(document);
addTitlePage(document);
addContent(document);
document.close();
pdfWriter.close();
OutputStream os = response.getOutputStream();
baosPDF.writeTo(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);
private static Font tableHeaderFont = new Font(Font.FontFamily.TIMES_ROMAN, 10,Font.BOLD);
public static String[][] FONTS = {{"c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H}};
private static void addMetaData(Document document) {
document.addTitle("Phonebook");
document.addSubject("phonebook");
document.addKeywords("phonebook");
document.addAuthor("John Smith");
document.addCreator("John Smith");
}
private static void addTitlePage(Document document) throws DocumentException {
Paragraph preface = new Paragraph();
addEmptyLine(preface, 1);
preface.add(new Paragraph("Phonebook", catFont));
addEmptyLine(preface, 1);
preface.add(new Paragraph("Generated: " + new Date(), smallBold));
addEmptyLine(preface, 1);
document.add(preface);
}
private void addContent(Document document) throws DocumentException {
Paragraph paragraph = new Paragraph();
addEmptyLine(paragraph, 1);
// Add a table
createTable(paragraph);
// Now add all this to the document
document.add(paragraph);
}
private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
paragraph.add(new Paragraph(" "));
}
}
private void createTable(Paragraph paragraf) throws DocumentException {
PdfPTable table = new PdfPTable(9);
table.setTotalWidth(new float[]{ 58, 50, 95, 60, 60, 135, 60, 42, 30 });
table.setLockedWidth(true);
List<User> listOfAllUsers = phoneBookSer.fetchAllUsers();
PdfPCell c1 = new PdfPCell(new Phrase("Prezime", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Ime", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Organizacijska jedinica", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Telefon", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Telefaks", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("E-mail", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Lokacija", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Kat", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Soba", tableHeaderFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
BaseFont bf = null;
try {
bf = BaseFont.createFont(FONTS[0][0], FONTS[0][1], BaseFont.EMBEDDED);
} catch (IOException e) {
e.printStackTrace();
}
Font fontArial = new Font(bf, 9);
//Automatically detects fonts
//FontFactory.registerDirectories();
//Font fontArial9 = FontFactory.getFont("Arial", 9);
for (User user : listOfAllUsers) {
PdfPCell cell1 = new PdfPCell(new Phrase(user.getPrezime(), fontArial));
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase(user.getIme(), fontArial));
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Phrase(user.getOrganizacijskaJedinica(), fontArial));
table.addCell(cell3);
PdfPCell cell4 = new PdfPCell(new Phrase(user.getTelefon(), fontArial));
table.addCell(cell4);
PdfPCell cell5 = new PdfPCell(new Phrase(user.getTelefaks(), fontArial));
table.addCell(cell5);
PdfPCell cell6 = new PdfPCell(new Phrase(user.getEmail(), fontArial));
table.addCell(cell6);
PdfPCell cell7 = new PdfPCell(new Phrase(user.getLokacija(), fontArial));
table.addCell(cell7);
PdfPCell cell8 = new PdfPCell(new Phrase(user.getKat(), fontArial));
table.addCell(cell8);
PdfPCell cell9 = new PdfPCell(new Phrase(user.getBrojSobe(), fontArial));
table.addCell(cell9);
}
paragraf.add(table);
}