我是一个非常简单的用例,其中,我需要上传一个jpeg&从rest api下载pdf。我面临下面提到的问题&我尽了最大的努力(据我所知)试图从SO& S中获得解决方案。谷歌,但我还没有成功。
问题:
sendPdf方法确实发送了多个数据片段(通过运行pkt嗅探器确认)但我在游乐场中的代码收到的响应为nil。 用例,但没有白费:
1尝试content-type = application / pdf 2尝试content-type = application / octet-stream
SaveFile方法确实创建了文件即。 img_1& img_2,但是图片查看器无法打开照片,这意味着在将字节流重新编译为pdf时会出现一些不匹配的比特。 用例,但没有白费:
1尝试过DataInputStream 2与ByteArrayInputStream一起尝试ImageIO功能
休息api:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
createOutput(request, response);
}
private void createOutput(HttpServletRequest request, HttpServletResponse response) {
PrintWriter out_response = null;
response.setHeader("Cache-Control", "no-cache");
saveFile(request);
sendPdf(response);
}
private void sendPdf(HttpServletResponse response) throws IOException {
//response.setContentType("application/pdf");
response.setContentType("application/octet-stream");
FileInputStream fin = new FileInputStream("C:\\Users\\dwalia\\Desktop\\abc.pdf");
if(fin !=null){
BufferedInputStream bin = new BufferedInputStream(fin);
BufferedOutputStream bout = new BufferedOutputStream(response.getOutputStream());
int ch =0;
while((ch=bin.read())!=-1)
{
bout.write(ch);
}
bin.close();
fin.close();
bout.close();
}
}
private void saveFile(HttpServletRequest request) {
byte[] data = new byte[0];
byte[] buffer = new byte[512];
int bytesRead;
try {
DataInputStream din = new DataInputStream(request.getInputStream());
while ((bytesRead = din.read(buffer)) > 0) {
byte[] newData = new byte[data.length + bytesRead];
System.arraycopy(data, 0, newData, 0, data.length);
System.arraycopy(buffer, 0, newData, data.length, bytesRead);
data = newData;
}
FileOutputStream fos = new FileOutputStream("C:\\Users\\dwalia\\Desktop\\img_1.png");
fos.write(data);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
File outputfile = new File("C:\\Users\\dwalia\\Desktop\\img_2.png");
if(img != null) ImageIO.write(img, "png", outputfile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
My xcode playground code (SRWeb library : https://github.com/sraj/Swift-SRWebClient):
1. Trying to upload a png & download a pdf in a single go :
var image_src = UIImage(named : "India")
var params = ["username":"uname", "password":"pwd"]
var img_data = UIImageJPEGRepresentation(image_src, 100)
SRWebClient.POST("http://169.254.54.114:8080/sample/hello_world_servlet")
.data(img_data, fieldName: "signature", data: params)
.send(
{(response:AnyObject!, status:Int) -> Void in
println(status) <-- 200
if let response_json: AnyObject = response {
println(response as! String) <-- nothing, response == nil
}
}
)
2. Trying to download pdf only :
SRWebClient.GET("http://169.254.54.114:8080/sample/hello_world_servlet",
success:{
(response:AnyObject!, status:Int) -> Void in
println("\(status) \(response)") <-- "Data nil"
if let response_data: AnyObject = response {
println(response_data) <-- nothing, response == nil
}
}, failure:{
(error:NSError!) -> Void in
println(error.localizedDescription) <-- nothing
}
)