when I use docx4j to generate doc from HTML and output through java servlet, it works well on Windows system, I can download and open the doc file normally.
When I put the project on Linux server, I can also download the doc file, but when openging file, it alert that the file is broken. I must click the confirm and restore the file.then open it normally.
my core code is like this.
how can i get the same result as windows?
code in jsp:
String vhtml = DownHtml2DocUtil.replaceSvgData2Base64(request);
response.reset();
response.setContentType("application/octet-stream");//设置为字节流
OutputStream output = null;
try {
output = response.getOutputStream();
response.addHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis() + ".doc");
DownHtml2DocUtil.genDocFromHtml(vhtml, output);
} catch (Exception e) {
} finally {
try {
if (output != null) {
output.close();
}
} catch (Exception e) {
}
}
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
code in java like this:
public static void genDocFromHtml(String html, OutputStream out)
throws EMPException {
try {
WordprocessingMLPackage wordMLPackage;
wordMLPackage = WordprocessingMLPackage.createPackage();
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(
wordMLPackage);
wordMLPackage.getMainDocumentPart().getContent()
.addAll(XHTMLImporter.convert(html, "utf-8"));
// wordMLPackage.save(out); -- i tried both method
new Save(wordMLPackage).save(out);
} catch (InvalidFormatException e) {
throw new EMPException(e);
} catch (Docx4JException e) {
throw new EMPException(e);
} catch (Exception e) {
e.printStackTrace();
}
}
any suggestion will be appreciate , thanks anyway;
Related
I have a GWTP application to export some data to an .xlsx file using Apache POI. Here is my presenter code.
protected void exportTable(String selectedPublisher, String selectedTarget, Drilldown drilldown) {
this.excelServiceAsync.generateDataEnrichmentExport(selectedPublisher, new AsyncCallback<Void>() {
#Override
public void onSuccess(Void result) {
Window.open(GWT.getModuleBaseURL() + "rpc/excelDownload", "_blank", "");
}
#Override
public void onFailure(Throwable caught) {
Window.alert("Error!");
}
});
}
Here is my excel generation code which is asynchronously called by the presenter.
workbook = new XSSFWorkbook();
sheetOne = workbook.createSheet("Export One");
// TODO Typical POI coding stuff
FileOutputStream out = null;
try {
out = new FileOutputStream(new File("file.xlsx"));
workbook.write(out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
And here is my controller which I use to download the excel file.
#ResponseBody
#RequestMapping(value = "/excelDownload")
public String downloadExcel(HttpServletRequest request, HttpServletResponse response){
File file = new File("file.xlsx");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename="
+ "file.xlsx");
response.setHeader("Content-Length", String.valueOf(file.length()));
FileInputStream is = null;
try {
is = new FileInputStream(file);
IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
response.getOutputStream().close();
} catch (FileNotFoundException e) {
logger.error(e.getMessage(), e.getCause());
} catch (IOException e) {
logger.error(e.getMessage(), e.getCause());
} finally {
try {
is.close();
} catch (Exception ex) {
return "redirect:/error/internal";
}
}
return "";
}
I'm generating the excel file and putting on the output stream. Then in the controller I get the data from the output stream to download as an .xlsx file. This isn't working. The working file is generated and saved on the server. But the file that is downloaded by the browser is a corrupted file. I'm not really sure why. Please help!
You forgot to close your FileOutputStream out. Maybe try using the try (resource) {...} syntax of Java7?
Try set response.setContentLength((int) file.length());
I have a problem that I do not see any sense at all with it... I am developing a Java application with Windows but then I deploy it on Ubuntu. It gets a pdf file from an FTP server and keeps it. When I do it with Windows the pdf file is perfect, but when is deployed on Ubuntu, the file is not download correctly. It is a pdf, I can open it and it has some parts of the document that seems to be the same, but more than the 80% of it is too dark or almost white. It happens with all the documents. This is my code:
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ftpUrl, ftpPort);
boolean login = ftpClient.login(ftpUser, ftpPsw);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
if (login) {
FileOutputStream fos = new FileOutputStream("pdffile.pdf");
if (ftpClient.retrieveFile("pdffile.pdf", fos)) {
System.out.println("File downloaded");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
} if (ftpClient != null) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Thank you very much :)
So I'm uploading a file to my VPS (Linux Centos 5 64 bit) via FTP using Java. The code I'm using to upload to my VPS is
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect(serverip);
client.login("user, pass);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
// Create an InputStream of the file to be uploaded
String filename = Shared.saveLocation + Shared.saveAs;
fis = new FileInputStream(filename);
// Store file to server
client.storeFile(Shared.saveAs, fis);
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
Now the code is working but what I want is to change where on the VPS it uploads the file to. Right now it's
serverip/HERE
I have some files so want to change it to
serverip/file/HERE
How can I go about doing so?
You can use the changeCurrentWorkingDirectory() method to change to the desired directory. Once you're in there, you can write the file using storeFile just like before.
changeCurrentWorkingDirectory returns true if the directory change was successful, otherwise it returns false. It takes a string which interpreted as the directory path. If the path starts with a slash, it's interpreted as absolute path starting at the ftproot directory. Otherwise it's interpreted as relative path.
Revised code could look something like this:
FTPClient client = new FTPClient();
FileInputStream fis = null;
try
{
client.connect(serverip);
client.login("user, pass);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
// change directory to serverip/file/
if (client.changeWorkingDirectory("/file"))
{
// Create an InputStream of the file to be uploaded
String filename = Shared.saveLocation + Shared.saveAs;
fis = new FileInputStream(filename);
// Store file to server
client.storeFile(Shared.saveAs, fis);
}
client.logout();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fis != null)
{
fis.close();
}
client.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
}
hey i want to upload a file to a directory called 'screenshots' on my webserver via FTP using java. I have been using this code and it says that it stores the file successfully and connected successfully but when i check my screenshots directory via the cpanel i dont see the file that was uploaded any help?
public static void uploadFilee() {
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("****************");
client.login("********", "********");
System.out.println("Connected Successfully");
String filename = "C:/Users/Christian/Desktop/screenshots/img_" + queueInfo.get("SessionID");
fis = new FileInputStream(filename);
client.storeFile(filename, fis);
System.out.println("Stored File Successfully");
client.logout();
} catch (IOException e) {
System.out.println("Error_1");
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
System.out.println("Error_2");
e.printStackTrace();
}
}
}
`
You may want to review this page
http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html#_storeFile(java.lang.String,java.lang.String,java.io.InputStream)
you have your statements such as storefile in a try statement, but if they fail, they return false, not an exception.
changing your code to inspect the return values of each function should help you find where your problem lies.
I am trying to telnet to a server, run a command and put the output of that command in a file.
I can get the command in the file but not the result of this command.
I cannot see my output on my console either, so I assumed it run but I am not sure.
Does anybody have any idea?
public final static void main(String[] args) throws IOException, InterruptedException
{
FileOutputStream fout = null;
try
{
fout = new FileOutputStream ("spyfile.log");
}
catch (IOException e)
{
System.err.println(
"Exception while opening the spy file: "
+ e.getMessage());
}
TelnetClient telnet;
telnet = new TelnetClient();
try
{
telnet.connect("myserver", 23);
}
catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}
telnet.registerSpyStream(fout);
PrintStream out = new PrintStream( telnet.getOutputStream() );
out.println( "mycommand" );
try
{
telnet.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}
fout.close();
System.exit(0);
}
I am not sure what is TelneClient, if this is Commons Net class, then you are missing the part that actually reads data exchanged during telnet session. Please run this example and see how it works, once you get it you'll be able to cut it to your needs.