I am using Ektorp (Java API for CouchDB) to store documents in my CouchDB instance. I am having trouble attaching images to documents. Every time I call createAttachment() it throws an ClientProtocolException.
Code Sample:
AttachmentInputStream attachment =
new AttachmentInputStream(attachmentId,
fileInputStream,
contentType,
file.length());
String rev = db.createAttachment(doc.getId(), attachment));
Does anyone know what's going wrong?
I had a similar problem using Ektorp. I resolved the issue by passing the latest revision number into the overloaded createAttachment method (db.createAttachment(doc.getId(), doc.getRevision(), attachment))). You could probably do the following:
AttachmentInputStream attachment =
new AttachmentInputStream(attachmentId,
fileInputStream,
contentType,
file.length());
String rev = db.createAttachment(doc.getId(), doc.getRevision(), attachment));
Good luck!
Related
I have about 100,000 BLOBs in my database and have to work with them. All is ok when someone tells me which type of BLOB I must deal with. But there will be situations when I will not know which type is it. So how can I find out which type my BLOB is?
Last time I handled BLOB I get specific info about it, it was zipped file. So I did this..
try {
byte[] str = this.jdbcTemplate.queryForObject("SELECT SAVEDATA FROM JDBEVPP1.TEVP005 WHERE GFNR = 357302", byte[].class); // pakira BLOB u byte array
ByteArrayInputStream bys = new ByteArrayInputStream(str);
GZIPInputStream gzip = new GZIPInputStream(bys);
//...etc...
}
How can I find out which type BLOB is using Java code?
The final answer to this question would be the comment of Robert Harvey:
"The usual way to identify a binary file of some type is to have some "magic numbers" at the beginning of the file that you can use to identify the type. See en.wikipedia.org/wiki/… and en.wikipedia.org/wiki/File_format#Magic_number"
And also comment of Erwin Smout:
"By reading the detailed specs of the database design. Absent that, by trying to locate the original author of the system and hoping he still remembers. Absent that, by trying to locate other code that uses the same BLOB and kind of re-engineering the spec from there. In most shops you will have to go all the way to the third step alas. "
We have a scenario in which we need to retrieve the description info for EC2 instances running on AWS. To accomplish this, we are using the AWS Java SDK. In 90% of our use case, the com.amazonaws.services.ec2.model.Instance class is exactly what we need. However, there is also a small use-case where it would be beneficial to get the raw XML describing the instance. That is, the XML data before it is converted into the Instance object. Is there any way to obtain both the Instance object and the XML string using the AWS Java SDK? Is there a way to manually convert from one to the other? Or, would we be forced to make a separate call using HttpClient or something similar to get the XML data?
Make an EC2Client by adding request handler and override the beforeUnmarshalling() method like below
AmazonEC2ClientBuilder.standard().withRegion("us-east-1")
.withRequestHandlers(
new RequestHandler2() {
#Override
public HttpResponse beforeUnmarshalling(Request<?> request, HttpResponse httpResponse) {
// httpResponse.getContent() is the raw xml response from AWS
// you either save it to a file or to a XML document
return new HTTPResponse(...);
// if you consumed httpResponse.getContent(), you need to provide new HTTPResponse
}
}
).build():
If you have xml (e.g. from using AWS rest API directly), then you can use com.amazonaws.services.ec2.model.transform.* classes to convert xml to java objects. Unfortunately, it only provides classes required for SDK itself. So you, for example, can convert raw XML to an Instance using InstanceStaxUnmarshaller, but can't convert Instance to XML unless you write such converter.
Here is an example how to parse an Instance XML:
XMLEventReader eventReader = XMLInputFactory.newInstance().createXMLEventReader(new StringReader(instanceXml));
StaxUnmarshallerContext suc = new StaxUnmarshallerContext(eventReader, new TreeMap<>());
InstanceStaxUnmarshaller isu = new InstanceStaxUnmarshaller();
Instance i = isu.unmarshall(suc);
System.out.println(i.toString());
You probably can try to intercept raw AWS response, so that you can keep raw XML while still using SDK most of the time. But I wouldn't call that easy as it will require quite a bit of coding.
You could use JAXB.marshal like following. JAXB (Java Architecture for XML Binding) could convert Java object to / from XML file.
StringWriter sw = new StringWriter();
JAXB.marshal(instance, sw);
String xmlString = sw.toString();
You can use AWS rest API to replace Java SDK. A bonus will be slight performance gain because you'll not send statistic data to Amazon as the SDK does.
Here is a live example of what I am talking about:
http://185.112.249.77:9999/Api/search?search=ж
That URL displays no results.
http://188.226.217.48:8338/api/clan_search/ж
It does display results.
How come this is happening?
My code for reading the parameter is: String search = request.getParameter("search");
System.out.println(search); also outputs a
?
I looked around and it seems there might be something I need to do in the Tomcat8 Config but I can't find what or figure out what has to be done.
I'd appreciate any help with this.
This problem also occurs when I am printing out the results. The first one shows no results and the 2nd shows the results and in UTF-8.
What is the most likely issue causing this and what code/config files would you need to see?
EDIT
I am receiving a bytearray which I am converting to an inputstream via a bytearrayinputstream like this.
InputStream myis = new ByteArrayInputStream(decryptedPayload);
I have a class which handles the packet and it extends a class I made called PacketInputStream. This class has a readString function which goes like this:
public String readString() throws IOException {
int length = readVarInt();
byte[] data = new byte[length];
readFully(data);
return new String(data, UTF8);
}
The string doesn't display properly on the returned byte[]s and it also doesn't work when I send it through a GET parameter.
Thanks
I got Spring 2.5.6 and I'm trying to implement some stupid functionality within.
I have a photo directory somewhere in my server machine and I want to expose these photos to the user. What is the common solution for this issue?
P.S.
By now I'm writing my photos to the response using my own controller, and that is not very handy for me, cause I want to keep my photo's url nice and friendly (for example like /myServlet/images/012345.jpg).
Having your own controller is a fine solution. Here is a simplified copy from my PictureController:
#RequestMapping("/pictures/{filename}.{extension}")
public void getPicture(#PathVariable String filename,
#PathVariable String extension, OutputStream outputStream,
HttpServletResponse response) {
DateTime cachePeriod = new DateTime();
cachePeriod = cachePeriod.plusDays(5);
response.setDateHeader("Expires", cachePeriod.getMillis());
pictureService.writePicture(filename + "." + extension, outputStream);
}
Where pictureService simply does IOUtils.copy(imageStream, outputStream)
I'm fetching a web page using the Apache httpcomponents Java library. After connecting the result I get is an HttpEntity which has a method getContent() which returns an InputStream and also has a method writeTo() which writes to an OutputStream.
I want to turn the result into a String for extracting information. What is the most elegant (and safe) way to do this?
Some possible solutions:
Write to a ByteArrayOutputStream and then convert those bytes to a String with a String constructor
use InputStreamReader to read straight from the stream, and put into a StringBuilder
Both of these feel a bit ugly. Would you recommend choosing one of these or something else?
System.out.println( EntityUtils.toString(httpResponse.getEntity()) );
What about (pseudo):
BasicResponseHandler handler = new org.apache.http.impl.client.BasicResponseHandler ();
String str = httpClient.execute(request, handler);
You would have to handle exceptions on your own in this case.
It may be ugly, but I think that's the only way to do it. You can use IOUtils.toString() from Commons-IO though without having to write your own code.