How to send PDF file from backend Java code? - java

I have a JSP file, there backend helper class to it. From the back end helper I need to send PDF file to the JSP as an attachment. How can I achieve that?

I would suggest you to use Apache Commons File Upload component. That's probably the best way rather than reinventing the wheel. ;)

Since you haven't told us if you're using any MVC framework or just plain Servlet, I'll go for the basics.
If you want to upload a file, Apache Commons File Upload is the best library that will translate your multipart/form-data encoded HttpServletRequest message and provide you with files uploaded (in InputStream format, mostly preferred).
It's up to you, the developer, to write the data back to a persistent storage of your choice.
The reverse, it's to take the file, get the appropriate MIME-Type, Content-Length (file size), and file data (InputStream, if possible) and render it back to the HttpServletResponse).
This code (fully functional and written by me) does put the file as attachment/inline.
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
/**
* #author Buhake Sindi (The Elite Gentleman)
* #since 01 September 2011
*/
public class FileServletRenderer implements ServletRenderer {
private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB
private static final String OCTECT_STREAM_MIME_TYPE = "application/octect-stream";
private String contentType = OCTECT_STREAM_MIME_TYPE;
private long contentLength;
private String contentDisposition = "inline";
private String fileName;
private InputStream inputStream;
/**
* #return the contentType
*/
public String getContentType() {
return contentType;
}
/**
* #param contentType
* the contentType to set
*/
public void setContentType(String contentType) {
this.contentType = contentType;
}
/**
* #return the contentLength
*/
public long getContentLength() {
return contentLength;
}
/**
* #param contentLength
* the contentLength to set
*/
public void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
/**
* #return the contentDisposition
*/
public String getContentDisposition() {
return contentDisposition;
}
/**
* #param contentDisposition
* the contentDisposition to set
*/
public void setContentDisposition(String contentDisposition) {
this.contentDisposition = contentDisposition;
}
/**
* #return the fileName
*/
public String getFileName() {
return fileName;
}
/**
* #param fileName
* the fileName to set
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* #return the inputStream
*/
public InputStream getInputStream() {
return inputStream;
}
/**
* #param inputStream
* the inputStream to set
*/
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public void setFile(File file) throws IOException {
if (file == null) {
throw new IOException("file is null.");
}
setInputStream(new BufferedInputStream(new FileInputStream(file)));
setContentLength(file.length());
}
/*
* (non-Javadoc)
*
* #see org.bfs.bayweb.util.renderer.ServletViewRenderer#render(javax.servlet.
* ServletRequest, javax.servlet.ServletResponse)
*/
public void render(ServletRequest request, ServletResponse response) throws IOException {
// TODO Auto-generated method stub
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
try {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int inputStreamLength = 0;
int length = 0;
if (contentType == null) {
contentType = request.getServletContext().getMimeType(getFileName());
}
//We couldn't determine Content-Type
if (contentType == null) {
contentType = OCTECT_STREAM_MIME_TYPE;
}
while ((length = getInputStream().read(buffer)) > 0) {
inputStreamLength += length;
bos.write(buffer, 0, length);
}
if (inputStreamLength != getContentLength()) {
setContentLength(inputStreamLength);
}
if (response instanceof HttpServletResponse) {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.reset();
httpResponse.setHeader("Content-Type", getContentType());
httpResponse.setHeader("Content-Length", String.valueOf(getContentLength()));
httpResponse.setHeader("Content-Disposition", "\"" + getContentDisposition() + "\""
+ ((getFileName() != null && !getFileName().isEmpty()) ? "; filename=\"" + getFileName() + "\"" : ""));
httpResponse.setHeader("Content-Type", getContentType());
}
// finally
bos.flush();
// clear
} finally {
// TODO Auto-generated catch block
close(bos);
close(getInputStream());
}
}
private void close(Closeable resource) throws IOException {
if (resource != null) {
resource.close();
}
}
}
The most important method is render(HttpServletRequest, HttpServletResponse).

Related

Using third party library in software ag webmethods results in InvocationTargetException

I'm using This Library in webmethods to read from a large .xlsm file, It's a wrapper around Apache POI library. I cannot use the default POI API since the file contains more than 1 million rows and it's just too big to be loaded at once.
so the problem here is that when I import the library in my test project in eclipse (not webmethods) it works perfectly without any problem but when I import it into webmethods with all the required jar files when I run, it throws an "InvocationTargetException".
Here's the class that is used for parsing from that library:
package com.monitorjbl.xlsx;
import com.monitorjbl.xlsx.exceptions.CloseException;
import com.monitorjbl.xlsx.exceptions.MissingSheetException;
import com.monitorjbl.xlsx.exceptions.OpenException;
import com.monitorjbl.xlsx.exceptions.ReadException;
import com.monitorjbl.xlsx.impl.StreamingCell;
import com.monitorjbl.xlsx.impl.StreamingRow;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import static com.monitorjbl.xlsx.XmlUtils.document;
import static com.monitorjbl.xlsx.XmlUtils.searchForNodeList;
/**
* Streaming Excel workbook implementation. Most advanced features of POI are not supported.
* Use this only if your application can handle iterating through an entire workbook, row by
* row.
*/
public class StreamingReader implements Iterable<Row>, AutoCloseable {
private static final Logger log = LoggerFactory.getLogger(StreamingReader.class);
private final SharedStringsTable sst;
private final StylesTable stylesTable;
private final XMLEventReader parser;
private final DataFormatter dataFormatter = new DataFormatter();
private int rowCacheSize;
private List<Row> rowCache = new ArrayList<>();
private Iterator<Row> rowCacheIterator;
private String lastContents;
private StreamingRow currentRow;
private StreamingCell currentCell;
private File tmp;
private StreamingReader(SharedStringsTable sst, StylesTable stylesTable, XMLEventReader parser, int rowCacheSize) {
this.sst = sst;
this.stylesTable = stylesTable;
this.parser = parser;
this.rowCacheSize = rowCacheSize;
}
/**
* Read through a number of rows equal to the rowCacheSize field or until there is no more data to read
*
* #return true if data was read
*/
private boolean getRow() {
try {
rowCache.clear();
while(rowCache.size() < rowCacheSize && parser.hasNext()) {
handleEvent(parser.nextEvent());
}
rowCacheIterator = rowCache.iterator();
return rowCacheIterator.hasNext();
} catch(XMLStreamException | SAXException e) {
log.debug("End of stream");
}
return false;
}
/**
* Handles a SAX event.
*
* #param event
* #throws SAXException
*/
private void handleEvent(XMLEvent event) throws SAXException {
if(event.getEventType() == XMLStreamConstants.CHARACTERS) {
Characters c = event.asCharacters();
lastContents += c.getData();
} else if(event.getEventType() == XMLStreamConstants.START_ELEMENT) {
StartElement startElement = event.asStartElement();
String tagLocalName = startElement.getName().getLocalPart();
if("row".equals(tagLocalName)) {
Attribute rowIndex = startElement.getAttributeByName(new QName("r"));
currentRow = new StreamingRow(Integer.parseInt(rowIndex.getValue()) - 1);
} else if("c".equals(tagLocalName)) {
Attribute ref = startElement.getAttributeByName(new QName("r"));
String[] coord = ref.getValue().split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
currentCell = new StreamingCell(CellReference.convertColStringToIndex(coord[0]), Integer.parseInt(coord[1]) - 1);
setFormatString(startElement, currentCell);
Attribute type = startElement.getAttributeByName(new QName("t"));
if(type != null) {
currentCell.setType(type.getValue());
} else {
currentCell.setType("n");
}
Attribute style = startElement.getAttributeByName(new QName("s"));
if(style != null){
String indexStr = style.getValue();
try{
int index = Integer.parseInt(indexStr);
currentCell.setCellStyle(stylesTable.getStyleAt(index));
} catch(NumberFormatException nfe) {
log.warn("Ignoring invalid style index {}", indexStr);
}
}
}
// Clear contents cache
lastContents = "";
} else if(event.getEventType() == XMLStreamConstants.END_ELEMENT) {
EndElement endElement = event.asEndElement();
String tagLocalName = endElement.getName().getLocalPart();
if("v".equals(tagLocalName)) {
currentCell.setRawContents(unformattedContents());
currentCell.setContents(formattedContents());
} else if("row".equals(tagLocalName) && currentRow != null) {
rowCache.add(currentRow);
} else if("c".equals(tagLocalName)) {
currentRow.getCellMap().put(currentCell.getColumnIndex(), currentCell);
}
}
}
/**
* Read the numeric format string out of the styles table for this cell. Stores
* the result in the Cell.
*
* #param startElement
* #param cell
*/
void setFormatString(StartElement startElement, StreamingCell cell) {
Attribute cellStyle = startElement.getAttributeByName(new QName("s"));
String cellStyleString = (cellStyle != null) ? cellStyle.getValue() : null;
XSSFCellStyle style = null;
if(cellStyleString != null) {
style = stylesTable.getStyleAt(Integer.parseInt(cellStyleString));
} else if(stylesTable.getNumCellStyles() > 0) {
style = stylesTable.getStyleAt(0);
}
if(style != null) {
cell.setNumericFormatIndex(style.getDataFormat());
String formatString = style.getDataFormatString();
if(formatString != null) {
cell.setNumericFormat(formatString);
} else {
cell.setNumericFormat(BuiltinFormats.getBuiltinFormat(cell.getNumericFormatIndex()));
}
} else {
cell.setNumericFormatIndex(null);
cell.setNumericFormat(null);
}
}
/**
* Tries to format the contents of the last contents appropriately based on
* the type of cell and the discovered numeric format.
*
* #return
*/
String formattedContents() {
switch(currentCell.getType()) {
case "s": //string stored in shared table
int idx = Integer.parseInt(lastContents);
return new XSSFRichTextString(sst.getEntryAt(idx)).toString();
case "inlineStr": //inline string (not in sst)
return new XSSFRichTextString(lastContents).toString();
case "str": //forumla type
return '"' + lastContents + '"';
case "e": //error type
return "ERROR: " + lastContents;
case "n": //numeric type
if(currentCell.getNumericFormat() != null && lastContents.length() > 0) {
return dataFormatter.formatRawCellContents(
Double.parseDouble(lastContents),
currentCell.getNumericFormatIndex(),
currentCell.getNumericFormat());
} else {
return lastContents;
}
default:
return lastContents;
}
}
/**
* Returns the contents of the cell, with no formatting applied
*
* #return
*/
String unformattedContents() {
switch(currentCell.getType()) {
case "s": //string stored in shared table
int idx = Integer.parseInt(lastContents);
return new XSSFRichTextString(sst.getEntryAt(idx)).toString();
case "inlineStr": //inline string (not in sst)
return new XSSFRichTextString(lastContents).toString();
default:
return lastContents;
}
}
/**
* Returns a new streaming iterator to loop through rows. This iterator is not
* guaranteed to have all rows in memory, and any particular iteration may
* trigger a load from disk to read in new data.
*
* #return the streaming iterator
*/
#Override
public Iterator<Row> iterator() {
return new StreamingIterator();
}
/**
* Closes the streaming resource, attempting to clean up any temporary files created.
*
* #throws com.monitorjbl.xlsx.exceptions.CloseException if there is an issue closing the stream
*/
#Override
public void close() {
try {
parser.close();
} catch(XMLStreamException e) {
throw new CloseException(e);
}
if(tmp != null) {
log.debug("Deleting tmp file [" + tmp.getAbsolutePath() + "]");
tmp.delete();
}
}
static File writeInputStreamToFile(InputStream is, int bufferSize) throws IOException {
File f = Files.createTempFile("tmp-", ".xlsx").toFile();
try(FileOutputStream fos = new FileOutputStream(f)) {
int read;
byte[] bytes = new byte[bufferSize];
while((read = is.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
is.close();
fos.close();
return f;
}
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
int rowCacheSize = 10;
int bufferSize = 1024;
int sheetIndex = 0;
String sheetName;
String password;
/**
* The number of rows to keep in memory at any given point.
* <p>
* Defaults to 10
* </p>
*
* #param rowCacheSize number of rows
* #return reference to current {#code Builder}
*/
public Builder rowCacheSize(int rowCacheSize) {
this.rowCacheSize = rowCacheSize;
return this;
}
/**
* The number of bytes to read into memory from the input
* resource.
* <p>
* Defaults to 1024
* </p>
*
* #param bufferSize buffer size in bytes
* #return reference to current {#code Builder}
*/
public Builder bufferSize(int bufferSize) {
this.bufferSize = bufferSize;
return this;
}
/**
* Which sheet to open. There can only be one sheet open
* for a single instance of {#code StreamingReader}. If
* more sheets need to be read, a new instance must be
* created.
* <p>
* Defaults to 0
* </p>
*
* #param sheetIndex index of sheet
* #return reference to current {#code Builder}
*/
public Builder sheetIndex(int sheetIndex) {
this.sheetIndex = sheetIndex;
return this;
}
/**
* Which sheet to open. There can only be one sheet open
* for a single instance of {#code StreamingReader}. If
* more sheets need to be read, a new instance must be
* created.
*
* #param sheetName name of sheet
* #return reference to current {#code Builder}
*/
public Builder sheetName(String sheetName) {
this.sheetName = sheetName;
return this;
}
/**
* For password protected files specify password to open file.
* If the password is incorrect a {#code ReadException} is thrown on
* {#code read}.
* <p>NULL indicates that no password should be used, this is the
* default value.</p>
*
* #param password to use when opening file
* #return reference to current {#code Builder}
*/
public Builder password(String password) {
this.password = password;
return this;
}
/**
* Reads a given {#code InputStream} and returns a new
* instance of {#code StreamingReader}. Due to Apache POI
* limitations, a temporary file must be written in order
* to create a streaming iterator. This process will use
* the same buffer size as specified in {#link #bufferSize(int)}.
*
* #param is input stream to read in
* #return built streaming reader instance
* #throws com.monitorjbl.xlsx.exceptions.ReadException if there is an issue reading the stream
*/
public StreamingReader read(InputStream is) {
File f = null;
try {
f = writeInputStreamToFile(is, bufferSize);
log.debug("Created temp file [" + f.getAbsolutePath() + "]");
StreamingReader r = read(f);
r.tmp = f;
return r;
} catch(IOException e) {
throw new ReadException("Unable to read input stream", e);
} catch(RuntimeException e) {
f.delete();
throw e;
}
}
/**
* Reads a given {#code File} and returns a new instance
* of {#code StreamingReader}.
*
* #param f file to read in
* #return built streaming reader instance
* #throws com.monitorjbl.xlsx.exceptions.OpenException if there is an issue opening the file
* #throws com.monitorjbl.xlsx.exceptions.ReadException if there is an issue reading the file
*/
public StreamingReader read(File f) {
try {
OPCPackage pkg;
if(password != null) {
// Based on: https://poi.apache.org/encryption.html
POIFSFileSystem poifs = new POIFSFileSystem(f);
EncryptionInfo info = new EncryptionInfo(poifs);
Decryptor d = Decryptor.getInstance(info);
d.verifyPassword(password);
pkg = OPCPackage.open(d.getDataStream(poifs));
} else {
pkg = OPCPackage.open(f);
}
XSSFReader reader = new XSSFReader(pkg);
SharedStringsTable sst = reader.getSharedStringsTable();
StylesTable styles = reader.getStylesTable();
InputStream sheet = findSheet(reader);
if(sheet == null) {
throw new MissingSheetException("Unable to find sheet at index [" + sheetIndex + "]");
}
XMLEventReader parser = XMLInputFactory.newInstance().createXMLEventReader(sheet);
return new StreamingReader(sst, styles, parser, rowCacheSize);
} catch(IOException e) {
throw new OpenException("Failed to open file", e);
} catch(OpenXML4JException | XMLStreamException e) {
throw new ReadException("Unable to read workbook", e);
} catch(GeneralSecurityException e) {
throw new ReadException("Unable to read workbook - Decryption failed", e);
}
}
InputStream findSheet(XSSFReader reader) throws IOException, InvalidFormatException {
int index = sheetIndex;
if(sheetName != null) {
index = -1;
//This file is separate from the worksheet data, and should be fairly small
NodeList nl = searchForNodeList(document(reader.getWorkbookData()), "/workbook/sheets/sheet");
for(int i = 0; i < nl.getLength(); i++) {
if(Objects.equals(nl.item(i).getAttributes().getNamedItem("name").getTextContent(), sheetName)) {
index = i;
}
}
if(index < 0) {
return null;
}
}
Iterator<InputStream> iter = reader.getSheetsData();
InputStream sheet = null;
int i = 0;
while(iter.hasNext()) {
InputStream is = iter.next();
if(i++ == index) {
sheet = is;
log.debug("Found sheet at index [" + sheetIndex + "]");
break;
}
}
return sheet;
}
}
class StreamingIterator implements Iterator<Row> {
public StreamingIterator() {
if(rowCacheIterator == null) {
hasNext();
}
}
#Override
public boolean hasNext() {
return (rowCacheIterator != null && rowCacheIterator.hasNext()) || getRow();
}
#Override
public Row next() {
return rowCacheIterator.next();
}
#Override
public void remove() {
throw new RuntimeException("NotSupported");
}
}
}
and here is the code I use in my java service: (it's basically what the library provides in their page as documentation)
File is = new File("E:\\bpc\\testdata01.xlsm"));
StreamingReader reader = StreamingReader.builder()
.rowCacheSize(100)
.bufferSize(4096)
.sheetName("SOURCE_1")
.read(is);
so when the StreamingReader calls read(is) function I get the below exception thrown:
java.lang.reflect.InvocationTargetException:Could not initialize class org.apache.poi.POIXMLTypeLoader
I'm really sure why is it happening like this... is it because the builder class in the source code is static and it's being called from my static java service?
Note: Please be noted that I'm running this software AG webmethods and it's has its own way of implementing a java service so the only place that I can put my code in is like this: (you cannot debug the java service)
public final class importInputFile_SVC
{
public static final void importInputFile(IData pipeline)
throws ServiceException {
//code goes here
}
}
Any help is highly appreciated.
Not enough information for really analyzing your problem. But try putting all libraries in your package's code/jars directory and enable the package classloader in the manifest.v3 file. This will ensure that your java service uses only your specific libs and not jars on other version which may be part of your wM installation.
Edit: Enable the package classloader in the manifest.v3 file by adding the following line:
<value name='classloader'>package</value>

How can I set an auto numeric name generator for image uploads, using a singleton pattern counter

So my Problem is that I have to create a Singleton pattern counter for numeric name giving. For example "1", "2", "3" etc. The idea is that every time i start the application and the Server(tomcat), it gets the last number and when I upload another image it should continue from there. Lets say the last one was "43", so the the next time I start the application it should know it and put "44" for the next image upload.
I'm not that good in Java so please give me some patience :)
This is my FileUploadServlet. It handles the request from the fileUploadForm.jsp by taking the file from the submit.
package upload;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import utils.FormatChecker;
import utils.UnzipFile;
//Servlet for handling the Upload request from the Index.jsp
#MultipartConfig
public class FileUploadServlet extends HttpServlet {
// Instace of the FileUpload object
private FileUploader uploader = new FileUploader();
// Instance of the FormatChecker object
private FormatChecker checker = new FormatChecker();
// Instance of the UnzipFile object
private UnzipFile unzip = new UnzipFile();
private static final long serialVersionUID = 1L;
private static final String SAVE_FOLDER = "C:\\Users\\cuche\\Desktop\\tomcat\\apache-tomcat-7.0.47\\webapps\\files";
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("error.jsp");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String contentType;
boolean isFormatValid;
Part filePart = request.getPart("file");
contentType = filePart.getContentType();
ServletContext context = getServletContext();
String appPath = context.getRealPath("/");
String fileNameOld = getFileName(filePart);
String fileNameNew = appPath + fileNameOld;
isFormatValid = checker.check(contentType);
pleas ignore the part with the FileUnziper
if (isFormatValid == true) {
if (contentType == ("application/x-zip-compressed")) {
unzip.FileUnziper(fileNameNew, SAVE_FOLDER);
} else {
//gets the content and saves in form of a stream
InputStream fileContent = filePart.getInputStream();
//using the uploadImage method of uploader class
uploader.uploadImage(fileNameNew, fileContent);
}
try {
response.sendRedirect("result.jsp");
} catch (IOException ex) {
response.getWriter().append(ex.getLocalizedMessage());
}
} else {
response.getWriter().append("Format is wrong");
}
}
// method for removing header for proper file upload
private String getFileName(Part part) {
for (String cd : part.getHeader("content-disposition").split(";")) {
if (cd.trim().startsWith("filename")) {
String filename = cd.substring(cd.indexOf('=') + 1).trim()
.replace("\"", "");
return filename.substring(filename.lastIndexOf('/') + 1)
.substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
}
}
return null;
}
}
This is my FileUploader class
package upload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* #author Iwan Cuche
* #date:
*/
public class FileUploader {
/**
* This method reads a File
*
* #param fileName
* #param stream
*/
public void uploadImage(String fileName, InputStream stream)
throws IOException {
try {
File file = new File(fileName);
OutputStream os = new FileOutputStream(file);
int data;
while ((data = stream.read()) != -1) {
os.write(data);
}
os.flush();
os.close();
System.out.println("Uploaded file successfully saved in "
+ file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
}
This is my Singleton class
package utils;
public class ServerCounter {
private static ServerCounter INSTANCE = new ServerCounter();
private ServerCounter() {};
public static ServerCounter getInstance() {
return INSTANCE;
}
}
I hope someone can help me because I'm not sure how to go at it.
In ServerCounter, add
private final AtomicLong counter = new AtomicLong();
public String nextval() { return String.valueOf(counter.incrementAndGet()); }
Each time you call INSTANCE.nextval() you'll get a fresh numeric string.
Clearly, each time you restart your application, the counter will restart.
ok, first you have to persist your counter if you want to get it after tomcat shutdown. we need listener for tomcat:
package utils;
public class ContextListener implements ServletContextListener{
void contextInitialized(ServletContextEvent sce){ // we could call loadFromFile here as well
}
//will be executed at tomcat shutdown
void contextDestroyed(ServletContextEvent sce){
ServerCounter .getInstance().writeToFile();
}
}
now the singleton(like in Marko's answer:)):
package utils;
public class ServerCounter {
private static ServerCounter INSTANCE = new ServerCounter();
private final AtomicLong counter;
private ServerCounter() {
//load value from file, do you need help by it?
long value = this.loadCounterFromFile();
counter = new AtomicLong(value);
};
private long loadCounterFromFile(){
BufferedReader br = null;
try {
//no problem if there is no file, we will return 0 in this case
br = new BufferedReader(new FileReader("C:\\Test\\counter.txt"));
String line = br.readLine();
if(line != null && line.length() > 0)
return Long.parseLong(line);
return 0;
//catch all exceptionse, because we could get NumberFormatException or FileNotFound from parseLong
} catch (Exception e) {
return 0;
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static ServerCounter getInstance() {
return INSTANCE;
}
public String nextval() { return String.valueOf(counter.incrementAndGet()); }
//will be executed by listener
public void writeToFile(){
//write the counter to file
writeToFile(counter.get());
}
private void writeToFile(long value){
try{
//you need folder c:\Test, file will be created automatically if there is no file, it will override the old file
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Test\\counter.txt"));
//need "" to create String
bw.write("" + value);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
now you can use ServerCounter.getInstance.nextval() to increment the counter and get the value
last thing is, you need to put the listener to your webApplication:
<web-app>
...
<listener>
<listener-class>utils.ContextListener </listener-class>
</listener>
</web-app>
EDIT: ServerCounter was implementing ServletContextListener by mistake
EDIT2: added read/write file

Retrieving email attachment filename with mime4j

I'm trying to use mime4j to parse emails, all is working fine, however I'm not able to get the file name of the attachment. Unfortunately the BodyDescriptor doesn't include this information in the content disposition, or content type fields.
I have read that the MaximalBodyDescriptor will include the filename, however I don't know how to tell the parser to return a MaximalBodyDescriptor object.
My handler is implementing the ContentHandler interface. I can't see an alternate interface which would work.
Any advice appreciated.
Here is a helper class that we use successfully to parse e-mails with their attachments.
In this approach attach.getFileName() has the filename.
package com.bitplan.smartCRM;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.io.IOUtils;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.dom.Body;
import org.apache.james.mime4j.dom.Entity;
import org.apache.james.mime4j.dom.Message;
import org.apache.james.mime4j.dom.MessageBuilder;
import org.apache.james.mime4j.dom.MessageServiceFactory;
import org.apache.james.mime4j.dom.Multipart;
import org.apache.james.mime4j.dom.SingleBody;
import org.apache.james.mime4j.dom.TextBody;
import org.apache.james.mime4j.dom.address.MailboxList;
import org.apache.james.mime4j.message.MessageImpl;
import org.apache.james.mime4j.stream.Field;
import com.bitplan.restinterface.Configuration;
/**
* EMail Helper class
*
* #author wf
* #author Denis Lunev <den#mozgoweb.com>
* #see http
* ://www.mozgoweb.com/posts/how-to-parse-mime-message-using-mime4j-library
* /
*/
public class EMailHelper implements ClipboardOwner {
public static boolean debug = true;
public static Logger LOGGER = Logger
.getLogger("com.bitplan.common.EMailHelper");
private StringBuffer txtBody;
private StringBuffer htmlBody;
private ArrayList<Entity> attachments;
/**
* get a String from an input Stream
*
* #param inputStream
* #return
* #throws IOException
*/
public String fromInputStream(InputStream inputStream) throws IOException {
String result = IOUtils.toString(inputStream);
// System.out.println(result);
return result;
}
/**
* get the full Mail from a message
*
* #param message
* #return
* #throws MessagingException
* #throws IOException
*/
public String fullMail(javax.mail.Message message) throws MessagingException,
IOException {
StringBuffer sBuf = new StringBuffer();
#SuppressWarnings("unchecked")
Enumeration<javax.mail.Header> headers = message.getAllHeaders();
while (headers.hasMoreElements()) {
javax.mail.Header header = headers.nextElement();
sBuf.append(header.getName() + ": " + header.getValue() + "\n");
}
sBuf.append(fromInputStream(message.getInputStream()));
return sBuf.toString();
}
/**
* Authentication
*/
public static class Authentication {
enum AuthenticationType {
pop3, smtp
};
String host;
String user;
String password;
AuthenticationType authenticationType;
Transport mTransport;
/**
* create an Authentication from the configuration
*
* #param configuration
* #param pAuthType
*/
public Authentication(Configuration configuration,
AuthenticationType pAuthType) {
authenticationType = pAuthType;
String prefix = pAuthType.name() + ".";
// use prefix e.g. pop3.host / smtp.host
host = (String) configuration.toMap().get(prefix + "host");
user = (String) configuration.toMap().get(prefix + "user");
password = (String) configuration.toMap().get(prefix + "password");
}
/**
* authenticate for sending / receiving e-mail
*
* #throws MessagingException
*/
public Transport authenticate() throws MessagingException {
Properties lProps = new Properties();
Session session = Session.getDefaultInstance(lProps);
switch (authenticationType) {
case pop3:
Store store = session.getStore("pop3");
store.connect(host, user, password);
store.close();
return null;
case smtp:
// http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html
mTransport = session.getTransport("smtp");
mTransport.connect(host, user, password);
return mTransport;
}
return null;
}
}
/**
* send the given e-mail
*
* #param email
* #throws MessagingException
*/
public void send(EMail email, Configuration configuration)
throws MessagingException {
Authentication lAuth = new Authentication(configuration,
Authentication.AuthenticationType.pop3);
Properties lProps = System.getProperties();
lProps.put("mail.smtp.host", lAuth.host);
// WF 2004-09-18: make sure full qualified domain name is used for localhost
// the default InetAddress.getLocalHost().getHostName() might not work ...
// lProps.put("mail.smtp.localhost",java.net.InetAddress.getLocalHost().getCanonicalHostName());
Session lSession = Session.getInstance(lProps);
MimeMessage lMsg = new MimeMessage(lSession);
lMsg.setFrom(new InternetAddress(email.getFromAdr()));
lMsg.setRecipients(javax.mail.Message.RecipientType.TO,
InternetAddress.parse(email.getToAdr()));
if (email.getCC() != null)
lMsg.setRecipients(javax.mail.Message.RecipientType.CC,
InternetAddress.parse(email.getCC()));
/*
* if (bcc()!=null) lMsg.setRecipients(Message.RecipientType.BCC,
* InternetAddress.parse(bcc())); lMsg.setHeader("X-Mailer", "JavaMail");
* lMsg.setSentDate(new Date()); lMsg.setSubject(subject());
* lMsg.setText(content()); lMsg.saveChanges(); Transport
* lTransport=lAuth.authenticate(); if (lTransport!=null)
* lTransport.sendMessage(lMsg,lMsg.getAllRecipients()); } else {
* Transport.send(lMsg); }
*/
}
/**
* retrieve pop3 mail from the given host
*
* #param pop3host
* #param user
* #param password
* #throws Exception
*/
public List<EMail> retrievePop3Mail(EMailManager eMailmanager,
Configuration configuration) throws Exception {
List<EMail> result = new ArrayList<EMail>();
Properties lProps = new Properties();
Session session = Session.getDefaultInstance(lProps);
Store store = session.getStore("pop3");
File attachmentDirectory = (File) configuration.toMap().get(
"attachmentDirectory");
// get a pop3 authentication
Authentication auth = new Authentication(configuration,
Authentication.AuthenticationType.pop3);
store.connect(auth.host, auth.user, auth.password);
Folder remoteInbox = store.getFolder("INBOX");
remoteInbox.open(Folder.READ_WRITE);
javax.mail.Message message[] = remoteInbox.getMessages();
if (message.length > 0) {
// get all messages
LOGGER.log(Level.INFO, "Getting " + message.length
+ " messages from POP3 Server '" + store.getURLName() + "'");
for (int i = 0; i < message.length; i++) {
if (!message[i].isSet(Flags.Flag.DELETED)) {
EMail email = eMailmanager.create();
String mailInput = this.fullMail(message[i]);
// System.out.print(mailInput);
ByteArrayInputStream mailStream = new ByteArrayInputStream(
mailInput.getBytes());
this.parseMessage(email, mailStream, attachmentDirectory);
result.add(email);
message[i].setFlag(Flags.Flag.DELETED, true);
}
} // for
} // if
remoteInbox.close(true);
store.close();
return result;
}
/**
* parse the Message into the given EMail
*
* #param email
* #param fileName
* #param attachmentDirectory
*
* #throws Exception
*/
public void parseMessage(EMail email, String fileName,
String attachmentDirectory) throws Exception {
parseMessage(email, new File(fileName), new File(attachmentDirectory));
}
/**
* strip the brackets
*
* #param addressList
* #return
*/
public String stripBrackets(MailboxList addressList) {
String result = null;
if (addressList != null) {
result = addressList.toString();
if (result.startsWith("[") && result.endsWith("]")) {
result = result.substring(1, result.length() - 1);
}
}
return result;
}
/**
* parse the Message from the given file into the given e-mail using the given
* attachmentDirectory
*
* #param email
* #param file
* #param attachmentDirectory
* #throws Exception
*/
public void parseMessage(EMail email, File file, File attachmentDirectory)
throws Exception {
if (!file.canRead() || (!file.isFile()))
throw new IllegalArgumentException(file.getCanonicalPath()
+ " is not a readable file");
// Get stream from file
FileInputStream fis = new FileInputStream(file);
parseMessage(email, fis, attachmentDirectory);
}
/**
* parse the Message from the given file into the given e-mail using the given
* attachmentDirectory
*
* #param email
* #param emailInputStream
* #param attachmentDirectory
* #throws Exception
*/
public void parseMessage(EMail email, InputStream eMailInputStream,
File attachmentDirectory) throws Exception {
Message mimeMsg = null;
if (!attachmentDirectory.isDirectory())
throw new IllegalArgumentException(attachmentDirectory.getCanonicalPath()
+ " is not a directory");
txtBody = new StringBuffer();
htmlBody = new StringBuffer();
attachments = new ArrayList<Entity>();
Exception ex = null;
try {
// Create message with stream from file
// If you want to parse String, you can use:
// Message mimeMsg = new Message(new
// ByteArrayInputStream(mimeSource.getBytes()));
MessageServiceFactory factory = MessageServiceFactory.newInstance();
MessageBuilder msgBuilder = factory.newMessageBuilder();
try {
mimeMsg = msgBuilder.parseMessage(eMailInputStream);
} catch (Throwable th) {
LOGGER.log(Level.SEVERE,th.getClass().getName());
LOGGER.log(Level.SEVERE,th.getMessage());
}
if (mimeMsg == null) {
LOGGER.log(Level.SEVERE, "could not read mime msg:\n",
this.fromInputStream(eMailInputStream));
return;
}
// Get some standard headers
if (mimeMsg.getTo() != null)
email.setToAdr(stripBrackets(mimeMsg.getTo().flatten()));
email.setFromAdr(stripBrackets(mimeMsg.getFrom()));
email.setSubject(mimeMsg.getSubject());
email.setSendDate(mimeMsg.getDate());
email.setEMailId(mimeMsg.getMessageId());
LOGGER.log(Level.INFO, "To: " + email.getToAdr());
LOGGER.log(Level.INFO, "From: " + email.getFromAdr());
LOGGER.log(Level.INFO, "Subject: " + mimeMsg.getSubject());
// Get custom header by name
Field priorityFld = mimeMsg.getHeader().getField("X-Priority");
// If header doesn't found it returns null
if (priorityFld != null) {
// Print header value
LOGGER.log(Level.FINEST, "Priority: " + priorityFld.getBody());
}
// If message contains many parts - parse all parts
if (mimeMsg.isMultipart()) {
Multipart multipart = (Multipart) mimeMsg.getBody();
parseBodyParts(multipart);
// fix mime4j 0.7.2 behaviour to have no separate text/plain part
if (txtBody.length() == 0) {
txtBody.append(multipart.getPreamble());
}
} else {
// If it's single part message, just get text body
String text = getTxtPart(mimeMsg);
txtBody.append(text);
}
email.setContent(txtBody.toString());
// Print text and HTML bodies
if (debug) {
LOGGER.log(Level.FINEST, "Text body: " + txtBody.toString());
LOGGER.log(Level.FINEST, "Html body: " + htmlBody.toString());
}
// loop over attachments
for (Entity attach : attachments) {
writeAttachment(attach, attachmentDirectory);
}
} catch (Exception cex) {
ex = cex;
} finally {
if (eMailInputStream != null) {
try {
eMailInputStream.close();
} catch (IOException ioex2) {
ioex2.printStackTrace();
}
}
}
if (ex != null) {
throw ex;
}
}
/**
* write the given Attachment
*
* #param attach
* #param attachmentDirectory
* #throws IOException
*/
public void writeAttachment(Entity attach, File attachmentDirectory)
throws IOException {
String attName = attach.getFilename();
// Create file with specified name
if (attName == null) {
LOGGER.log(Level.WARNING, "attachment has no file name using 'attachment"
+ attach.hashCode() + "' instead");
attName = "attachment" + attach.hashCode();
}
FileOutputStream fos = new FileOutputStream(new File(attachmentDirectory,
attName));
try {
writeBody(fos, attach.getBody());
} finally {
fos.close();
}
}
/**
* write the given body to the given fileoutput stream
*
* #param fos
* #param body
* #throws IOException
*/
public void writeBody(FileOutputStream fos, Body body) throws IOException {
if (body instanceof SingleBody) {
((SingleBody) body).writeTo(fos);
} else if (body instanceof MessageImpl) {
writeBody(fos, ((MessageImpl) body).getBody());
} else {
LOGGER.log(Level.WARNING, "can't handle body of type "
+ body.getClass().getSimpleName());
}
}
/**
* This method classifies bodyPart as text, html or attached file
*
* #param multipart
* #throws IOException
*/
private void parseBodyParts(Multipart multipart) throws IOException {
// loop over the parts
for (Entity part : multipart.getBodyParts()) {
String mimeType = part.getMimeType();
if (mimeType.equals("text/plain")) {
String txt = getTxtPart(part);
txtBody.append(txt);
} else if (mimeType.equals("text/html")) {
String html = getTxtPart(part);
htmlBody.append(html);
} else if (part.getDispositionType() != null
&& !part.getDispositionType().equals("")) {
// If DispositionType is null or empty, it means that it's multipart,
// not attached file
attachments.add(part);
}
// If current part contains other, parse it again by recursion
if (part.isMultipart()) {
parseBodyParts((Multipart) part.getBody());
}
}
}
/**
*
* #param part
* #return
* #throws IOException
*/
private String getTxtPart(Entity part) throws IOException {
// Get content from body
TextBody tb = (TextBody) part.getBody();
return this.fromInputStream(tb.getInputStream());
}
/**
* Place a String on the clipboard, and make this class the owner of the
* Clipboard's contents.
*
* #param aString
*/
public void setClipboardContents(String aString) {
StringSelection stringSelection = new StringSelection(aString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, this);
}
/**
* get text from the clipboard
*
* #return
* #throws Exception
*/
public String getClipboardText() throws Exception {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
String text = (String) clipboard.getData(DataFlavor.stringFlavor);
return text;
}
/**
* get Mail from clipboard
*
* #param email
* #param attachmentDirectory
* #return
* #throws Exception
*/
public boolean getMailFromClipboard(EMail email, File attachmentDirectory)
throws Exception {
String mailText = getClipboardText();
if (mailText == null)
return false;
this.parseMessage(email,
new ByteArrayInputStream(mailText.getBytes("UTF-8")),
attachmentDirectory);
return true;
}
/*
* (non-Javadoc)
*
* #see
* java.awt.datatransfer.ClipboardOwner#lostOwnership(java.awt.datatransfer
* .Clipboard, java.awt.datatransfer.Transferable)
*/
#Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
}
}
I recommand you to use Token streams.
It is quite straight forward with it.
You can locate attachment by using headers of your multipart section :
Content-Disposition:attachment; filename="toto.txt"
You must be carefull when parsing a header with it ... It can be mail headers or multipart section header....

Receving data form using HttpURLConnection.getOutputStream,chinese encoding wrong(The same code,why the result is not the same?)

everybody:
I try to make a httpRequest for getting data,there's something wrong while response contain chinese characters.I got a String with HttpURLConnection.getInputStream()(see /test code./),I have wrote a main method on my local environment to test this code.And it works well!But when I startup my project,using this code for requesting data,the problem came out...(from the system.out below we can see the chinese characters are wrong )I don't understand why it cant be translate into write encoding.
And when I copy (/test code./) to static main method and run it,it seems no any problem,right?The same code,why the result is not the same?
my computer environment : win7,project and tomcat all use utf-8,request and response encoding both utf-8,too.
Here's my code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Vector;
public class HttpRequestUtil {
private String defaultContentEncoding;
public HttpRequestUtil() {
this.defaultContentEncoding = Charset.defaultCharset().name();
}
public HttpRequestUtil(String encoding) {
this.defaultContentEncoding = encoding;
}
/**
* 发送GET请求
*
* #param urlString
* URL地址
* #return 响应对象
* #throws IOException
*/
public HttpResponsUtil sendGet(String urlString) throws IOException {
return this.send(urlString, "GET", null, null);
}
/**
* 发送GET请求
*
* #param urlString
* URL地址
* #param params
* 参数集合
* #return 响应对象
* #throws IOException
*/
public HttpResponsUtil sendGet(String urlString, Map<String, String> params)
throws IOException {
return this.send(urlString, "GET", params, null);
}
/**
* 发送GET请求
*
* #param urlString
* URL地址
* #param params
* 参数集合
* #param propertys
* 请求属性
* #return 响应对象
* #throws IOException
*/
public HttpResponsUtil sendGet(String urlString, Map<String, String> params,
Map<String, String> propertys) throws IOException {
return this.send(urlString, "GET", params, propertys);
}
/**
* 发送POST请求
*
* #param urlString
* URL地址
* #return 响应对象
* #throws IOException
*/
public HttpResponsUtil sendPost(String urlString) throws IOException {
return this.send(urlString, "POST", null, null);
}
/**
* 发送POST请求
*
* #param urlString
* URL地址
* #param params
* 参数集合
* #return 响应对象
* #throws IOException
*/
public HttpResponsUtil sendPost(String urlString, Map<String, String> params)
throws IOException {
return this.send(urlString, "POST", params, null);
}
/**
* 发送POST请求
*
* #param urlString
* URL地址
* #param params
* 参数集合
* #param propertys
* 请求属性
* #return 响应对象
* #throws IOException
*/
public HttpResponsUtil sendPost(String urlString, Map<String, String> params,
Map<String, String> propertys) throws IOException {
return this.send(urlString, "POST", params, propertys);
}
/**
* 发送HTTP请求
*
* #param urlString
* #return 响映对象
* #throws IOException
*/
private HttpResponsUtil send(String urlString, String method,
Map<String, String> parameters, Map<String, String> propertys)
throws IOException {
HttpURLConnection urlConnection = null;
if (method.equalsIgnoreCase("GET") && parameters != null) {
StringBuffer param = new StringBuffer();
int i = 0;
for (String key : parameters.keySet()) {
if (i == 0)
param.append("?");
else
param.append("&");
param.append(key).append("=").append(parameters.get(key));
i++;
}
urlString += param;
}
URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod(method);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
if (propertys != null)
for (String key : propertys.keySet()) {
urlConnection.addRequestProperty(key, propertys.get(key));
}
if (method.equalsIgnoreCase("POST") && parameters != null) {
StringBuffer param = new StringBuffer();
for (String key : parameters.keySet()) {
param.append("&");
param.append(key).append("=").append(parameters.get(key));
}
urlConnection.getOutputStream().write(param.toString().getBytes());
urlConnection.getOutputStream().flush();
urlConnection.getOutputStream().close();
}
return this.makeContent(urlString, urlConnection);
}
/**
* 得到响应对象
*
* #param urlConnection
* #return 响应对象
* #throws IOException
*/
private HttpResponsUtil makeContent(String urlString,
HttpURLConnection urlConnection) throws IOException {
HttpResponsUtil httpResponser = new HttpResponsUtil();
try {
InputStream in = urlConnection.getInputStream();
//这里如果不指定InputStreamReader的编码,当中文字符超过两个时就会出现乱码(utf8情况下)
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(in, this.defaultContentEncoding));
httpResponser.contentCollection = new Vector<String>();
StringBuffer temp = new StringBuffer();
String line = bufferedReader.readLine();
while (line != null) {
httpResponser.contentCollection.add(line);
temp.append(line); //.append("\r\n")
line = bufferedReader.readLine();
}
bufferedReader.close();
String ecod = urlConnection.getContentEncoding();
if (ecod == null){
ecod = this.defaultContentEncoding;
httpResponser.content = temp.toString();
/*test code.*/
System.out.println("ecod is" + ecod);
//ecod isUTF-8
System.out.println("temp = " + temp.toString());
//temp = {"tags":[{"r_id":3553740735078269,"tag":"再测试"},{"r_id":3555331928010955,"tag":"再测试"},{"r_id":3555332033148401,"tag":"再测试,测试好多个字符串"}],"rids":"3555332033148401,3555331928010955,3553740735078269,3553509628288136,3552801450193593,3550638895838037,3550607615227068,3550334377352366,3550333965816322,3550315184067715,3550136490112256,3550098295918936,3550094449884887,3550094122996559,3550092986220398,3550090717120421,3550090532251655,3550089362475695,3549897523877864,3549756045751486,3549719639064459,3549564726035910,3549379539997223,3549377979585685,3549373109656624","idArr":["3555332033148401","3555331928010955","3553740735078269","3553509628288136","3552801450193593","3550638895838037","3550607615227068","3550334377352366","3550333965816322","3550315184067715","3550136490112256","3550098295918936","3550094449884887","3550094122996559","3550092986220398","3550090717120421","3550090532251655","3550089362475695","3549897523877864","3549756045751486","3549719639064459","3549564726035910","3549379539997223","3549377979585685","3549373109656624"],"uId":1916364215,"type":3}
System.out.println("temp after encode: "+new String(temp.toString().getBytes(), ecod));
//temp after encode: {"tags":[{"r_id":3553740735078269,"tag":"?????"},{"r_id":3555331928010955,"tag":"?????"},{"r_id":3555332033148401,"tag":"?????,??????????"}],"rids":"3555332033148401,3555331928010955,3553740735078269,3553509628288136,3552801450193593,3550638895838037,3550607615227068,3550334377352366,3550333965816322,3550315184067715,3550136490112256,3550098295918936,3550094449884887,3550094122996559,3550092986220398,3550090717120421,3550090532251655,3550089362475695,3549897523877864,3549756045751486,3549719639064459,3549564726035910,3549379539997223,3549377979585685,3549373109656624","idArr":["3555332033148401","3555331928010955","3553740735078269","3553509628288136","3552801450193593","3550638895838037","3550607615227068","3550334377352366","3550333965816322","3550315184067715","3550136490112256","3550098295918936","3550094449884887","3550094122996559","3550092986220398","3550090717120421","3550090532251655","3550089362475695","3549897523877864","3549756045751486","3549719639064459","3549564726035910","3549379539997223","3549377979585685","3549373109656624"],"uId":1916364215,"type":3}
String str = "{\"tags\":[{\"r_id\":3553740735078269,\"tag\":\"再测试\"},{\"r_id\":3555331928010955,\"tag\":\"再测试\"},{\"r_id\":3555332033148401,\"tag\":\"再测试,测试好多个字符串\"}],\"rids\":\"3555332033148401,3555331928010955,3553740735078269,3553509628288136,3552801450193593,3550638895838037,3550607615227068,3550334377352366,3550333965816322,3550315184067715,3550136490112256,3550098295918936,3550094449884887,3550094122996559,3550092986220398,3550090717120421,3550090532251655,3550089362475695,3549897523877864,3549756045751486,3549719639064459,3549564726035910,3549379539997223,3549377979585685,3549373109656624\",\"idArr\":[\"3555332033148401\",\"3555331928010955\",\"3553740735078269\",\"3553509628288136\",\"3552801450193593\",\"3550638895838037\",\"3550607615227068\",\"3550334377352366\",\"3550333965816322\",\"3550315184067715\",\"3550136490112256\",\"3550098295918936\",\"3550094449884887\",\"3550094122996559\",\"3550092986220398\",\"3550090717120421\",\"3550090532251655\",\"3550089362475695\",\"3549897523877864\",\"3549756045751486\",\"3549719639064459\",\"3549564726035910\",\"3549379539997223\",\"3549377979585685\",\"3549373109656624\"],\"uId\":1916364215,\"type\":3}";
System.out.println("compare two string:" + temp.toString().equals(str));
//compare two string:true
StringBuffer sb = new StringBuffer();
sb.append(str);
String s = new String(sb.toString().getBytes(), "UTF-8");
System.out.println("s test:" + s);
//s test:{"tags":[{"r_id":3553740735078269,"tag":"?????"},{"r_id":3555331928010955,"tag":"?????"},{"r_id":3555332033148401,"tag":"?????,??????????"}],"rids":"3555332033148401,3555331928010955,3553740735078269,3553509628288136,3552801450193593,3550638895838037,3550607615227068,3550334377352366,3550333965816322,3550315184067715,3550136490112256,3550098295918936,3550094449884887,3550094122996559,3550092986220398,3550090717120421,3550090532251655,3550089362475695,3549897523877864,3549756045751486,3549719639064459,3549564726035910,3549379539997223,3549377979585685,3549373109656624","idArr":["3555332033148401","3555331928010955","3553740735078269","3553509628288136","3552801450193593","3550638895838037","3550607615227068","3550334377352366","3550333965816322","3550315184067715","3550136490112256","3550098295918936","3550094449884887","3550094122996559","3550092986220398","3550090717120421","3550090532251655","3550089362475695","3549897523877864","3549756045751486","3549719639064459","3549564726035910","3549379539997223","3549377979585685","3549373109656624"],"uId":1916364215,"type":3}
/*test code.*/
} else{
httpResponser.content = new String(temp.toString().getBytes(), ecod);
}
httpResponser.urlString = urlString;
httpResponser.defaultPort = urlConnection.getURL().getDefaultPort();
httpResponser.file = urlConnection.getURL().getFile();
httpResponser.host = urlConnection.getURL().getHost();
httpResponser.path = urlConnection.getURL().getPath();
httpResponser.port = urlConnection.getURL().getPort();
httpResponser.protocol = urlConnection.getURL().getProtocol();
httpResponser.query = urlConnection.getURL().getQuery();
httpResponser.ref = urlConnection.getURL().getRef();
httpResponser.userInfo = urlConnection.getURL().getUserInfo();
httpResponser.contentEncoding = ecod;
httpResponser.code = urlConnection.getResponseCode();
httpResponser.message = urlConnection.getResponseMessage();
httpResponser.contentType = urlConnection.getContentType();
httpResponser.method = urlConnection.getRequestMethod();
httpResponser.connectTimeout = urlConnection.getConnectTimeout();
httpResponser.readTimeout = urlConnection.getReadTimeout();
return httpResponser;
} catch (IOException e) {
throw e;
} finally {
if (urlConnection != null)
urlConnection.disconnect();
}
}
/**
* 默认的响应字符集
*/
public String getDefaultContentEncoding() {
return this.defaultContentEncoding;
}
/**
* 设置默认的响应字符集
*/
public void setDefaultContentEncoding(String defaultContentEncoding) {
this.defaultContentEncoding = defaultContentEncoding;
}
public static void main(String[] args) throws Exception {
String str = "{\"tags\":[{\"r_id\":3553740735078269,\"tag\":\"再测试\"},{\"r_id\":3555331928010955,\"tag\":\"再测试\"},{\"r_id\":3555332033148401,\"tag\":\"再测试,测试好多个字符串\"}],\"rids\":\"3555332033148401,3555331928010955,3553740735078269,3553509628288136,3552801450193593,3550638895838037,3550607615227068,3550334377352366,3550333965816322,3550315184067715,3550136490112256,3550098295918936,3550094449884887,3550094122996559,3550092986220398,3550090717120421,3550090532251655,3550089362475695,3549897523877864,3549756045751486,3549719639064459,3549564726035910,3549379539997223,3549377979585685,3549373109656624\",\"idArr\":[\"3555332033148401\",\"3555331928010955\",\"3553740735078269\",\"3553509628288136\",\"3552801450193593\",\"3550638895838037\",\"3550607615227068\",\"3550334377352366\",\"3550333965816322\",\"3550315184067715\",\"3550136490112256\",\"3550098295918936\",\"3550094449884887\",\"3550094122996559\",\"3550092986220398\",\"3550090717120421\",\"3550090532251655\",\"3550089362475695\",\"3549897523877864\",\"3549756045751486\",\"3549719639064459\",\"3549564726035910\",\"3549379539997223\",\"3549377979585685\",\"3549373109656624\"],\"uId\":1916364215,\"type\":3}";
StringBuffer sb = new StringBuffer();
sb.append(str);
String s = new String(sb.toString().getBytes(), "UTF-8");
System.out.println("s test:" + s);
//s test:{"tags":[{"r_id":3553740735078269,"tag":"再测试"},{"r_id":3555331928010955,"tag":"再测试"},{"r_id":3555332033148401,"tag":"再测试,测试好多个字符串"}],"rids":"3555332033148401,3555331928010955,3553740735078269,3553509628288136,3552801450193593,3550638895838037,3550607615227068,3550334377352366,3550333965816322,3550315184067715,3550136490112256,3550098295918936,3550094449884887,3550094122996559,3550092986220398,3550090717120421,3550090532251655,3550089362475695,3549897523877864,3549756045751486,3549719639064459,3549564726035910,3549379539997223,3549377979585685,3549373109656624","idArr":["3555332033148401","3555331928010955","3553740735078269","3553509628288136","3552801450193593","3550638895838037","3550607615227068","3550334377352366","3550333965816322","3550315184067715","3550136490112256","3550098295918936","3550094449884887","3550094122996559","3550092986220398","3550090717120421","3550090532251655","3550089362475695","3549897523877864","3549756045751486","3549719639064459","3549564726035910","3549379539997223","3549377979585685","3549373109656624"],"uId":1916364215,"type":3}
}
}
I have find out the solution,use the code below:
httpResponser.content = new String(temp.toString().getBytes(this.defaultContentEncoding), ecod);

How to generate xml from xsd schema?

I am having xsd schema..How can I generate the xml by using this schema programmatically in java..? and it should be dynamic ,means I can give any schema.
Is there any library available to do same.?
I have alrady seen the other post also,but unfortunately it did not suit me.. please give tour ideas....???
I am not finding any approach to do same.
XPath is not a tool for generating XML. I'm afraid you're trying to accomplish your goal with the wrong tools. So, I think the answer to your question is: you can't.
Take a look at this link :
JAVA: Build XML document using XPath expressions
And go through this link under the XPath heading :
http://www.vogella.de/articles/JavaXML/article.html
I think this should help you out...:)
The following code performs validation as per your need,
and also tells you the line number where the error occurred during parsing
You will need following jars to run this code
Only code change needed is at validateSchema() method where request response args need to be replaced with a string representation of your xml and String representation of your XSD.
resolver.jar, xml-apis.jar, serializer.jar, xercesImpl.jar
import org.apache.xerces.parsers.SAXParser;
import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.NamespaceContext;
import org.apache.xerces.xni.XMLLocator;
import org.apache.xerces.xni.XNIException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Performs validation on XML based on the XSD. Overrides the
* {#link SAXParser}.
*
*
*/
public class ValidateSchema extends SAXParser {
/**
* Container for current line and coloum number being parsed in the XML.
*/
private XMLLocator locator;
/**
* Default public constructor.
*/
public ValidateSchema() {
super();
}
/**
* Used for obtaining the {#link XMLLocator} locator object.
*/
#Override
public void startDocument(
XMLLocator locator, String encoding, NamespaceContext namespaceContext, Augmentations augs)
throws XNIException {
this.locator = locator;
super.startDocument(locator, encoding, namespaceContext, augs);
}
/**
* Validates the XML against the provided XSD.
*
* #param req HttpServletRequest object.
* #param resp HttpServletResponse object.
* #throws IOException
*/
public void validateSchema(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String content = req.getParameter("data");
String selectbox = req.getParameter("selectbox");
content = content.trim();
// Convert the XML string to byte stream.
InputStream is = new ByteArrayInputStream(content.getBytes());
try {
this.setFeature(Constants.VALIDATION_PROP, true);
this.setFeature(Constants.SCHEMA_PROP, true);
this.setFeature(Constants.DYNAMIC_PROP, true);
this.setFeature(Constants.SCHEMA_CHECKING_PROP, true);
if("1".equalsIgnoreCase(selectbox)) {
this.setProperty(Constants.SCHEMA_LOC,"oem.xsd" );
} else if("2".equalsIgnoreCase(selectbox)) {
this.setProperty(Constants.SCHEMA_LOC,"carrier.xsd" );
}
Validator handler = new Validator();
this.setErrorHandler(handler);
InputSource isp = new InputSource();
isp.setByteStream(is);
isp.setEncoding("UTF-8");
this.parse(isp);
if (handler.validationError == true) {
StringBuffer errorMessage = new StringBuffer(512);
errorMessage
.append("<div style='background: #ffebe6;border: 0px solid #ffe0d7;color:#c10000;height:60px;padding: 5px;'>")
.append(Constants.INVALID_XML_DOCUMENT)
.append(" LineNo: ")
.append(handler.saxParseException.getLineNumber())
.append(" ColumnNo: ")
.append(handler.saxParseException.getColumnNumber())
.append("<br/>")
.append(handler.validationError)
.append(handler.saxParseException.getMessage())
.append("</div>");
System.out.println( errorMessage );
} else {
StringBuffer validMsg = new StringBuffer(512);
validMsg.append("<div style='background: #ebeff9;border: 0px solid #6b90da;height:60px;padding: 5px;'>")
.append(Constants.VALID_XML_DOCUMENT).append("</div>");
System.out.println( validMsg );
}
} catch (SAXException e) {
StringBuffer errorMessage = new StringBuffer(512);
errorMessage
.append("<div style='background: #ffebe6;border: 0px solid #ffe0d7;color:#c10000;height:60px;padding: 5px;'>")
.append(Constants.INVALID_XML_DOCUMENT)
.append(" LineNo: ")
.append(this.locator.getLineNumber())
.append(" ColumnNo: ")
.append(this.locator.getColumnNumber())
.append(" <br/>")
.append(e.getMessage())
.append("</div>");
System.out.println( errorMessage );
} catch (Exception e) {
StringBuffer errorMessage = new StringBuffer(512);
errorMessage
.append("<div style='background: #ffebe6;border: 1px solid #ffe0d7;color:#c10000;height:60px;padding: 5px;'>")
.append(Constants.INVALID_XML_DOCUMENT)
.append(" <br/>")
.append(e.getMessage())
.append("</div>");
System.out.println( errorMessage );
}
}
/**
* Writes back the response to client.
*
* #param msg Response message.
* #param resp HttpServletResponse object.
* #throws IOException
*/
private void responseWrite(
String msg, HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
resp.getWriter().write(msg);
}
/**
* Custom handler for Errors while parsing documents.
*/
private class Validator extends DefaultHandler {
public boolean validationError = false;
public SAXParseException saxParseException = null;
/**
* #throws SAXException
*/
#Override
public void error(SAXParseException exception) throws SAXException {
validationError = true;
saxParseException = exception;
}
/**
* #throws SAXException
*/
#Override
public void fatalError(SAXParseException exception) throws SAXException {
validationError = true;
saxParseException = exception;
}
/**
* #throws SAXException
*/
#Override
public void warning(SAXParseException exception) throws SAXException {
}
}
}
Constants.Java contains the validation properties that you need to specify
public final class Constants {
public static final String VALIDATION_PROP = "http://xml.org/sax/features/validation";
public static final String SCHEMA_PROP = "http://apache.org/xml/features/validation/schema";
public static final String DYNAMIC_PROP = "http://apache.org/xml/features/validation/dynamic";
public static final String SCHEMA_CHECKING_PROP =
"http://apache.org/xml/features/validation/schema-full-checking";
public static final String SCHEMA_LOC =
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";
public static final String VALID_XML_DOCUMENT = "The above XML is valid.";
public static final String INVALID_XML_DOCUMENT = "The Document has error at:";
}

Categories