Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Feb 12, 2013 05:24 PM by Ruchira
Member
129 Points
36 Posts
Feb 08, 2013 07:08 AM|LINK
Hi,
I work with Morena 6 scan java library and must to Handle POST operation with img/jpeg content: Here how to send data:
private class UploadImageAction extends AbstractAction implements Runnable { UploadImageAction() { super("upload to server"); } public void actionPerformed(ActionEvent event) { new Thread(this).start(); } public synchronized void run() { try { status.setText("Working ..."); Image image=selected.getImage(); ByteArrayOutputStream tmp=new ByteArrayOutputStream(); BufferedImage bufferedImage=new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); bufferedImage.createGraphics().drawImage(image, 0, 0, null); ImageIO.write(bufferedImage, "jpg", tmp); tmp.close(); int contentLength=tmp.size(); if (contentLength>1024*1024) throw new Exception("Image is too big to upload"); URL uploadURL=new URL(documentBase, "upload.php"); HttpURLConnection connection=(HttpURLConnection)uploadURL.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setUseCaches(false); connection.setDefaultUseCaches(false); connection.setRequestProperty("content-type", "img/jpeg"); connection.setRequestProperty("content-length", String.valueOf(contentLength)); OutputStream out=connection.getOutputStream(); out.write(tmp.toByteArray()); out.close(); InputStream in=connection.getInputStream(); int c; while ((c=in.read())!=-1) System.err.write(c); in.close(); URL imageURL=new URL(documentBase, connection.getHeaderField("file-name")); status.setText("Done - image is uploaded to "+imageURL+" (for at least 5 minutes) ..."); } catch (Throwable exception) { JOptionPane.showMessageDialog(MainPanel.this, exception.toString(), "Error", JOptionPane.ERROR_MESSAGE); exception.printStackTrace(); status.setText("Failed, try again ..."); } } public boolean isEnabled() { return hasServer && selected != null; } }
in PHP it is done by:
<?PHP $dh = opendir("."); while (false !== ($filename = readdir($dh))) { $dc[] = $filename; } foreach($dc as $f) { if (substr($f, 0, 4)=="img_") if (filectime($f)<time()-5*60) unlink($f); } $fn="img_".time().".jpg"; $fp=fopen($fn,"w"); fwrite($fp, $HTTP_RAW_POST_DATA); fclose($fp); header("file-name: ".$fn); ?>
I am trying do do same as PHP with ASP.NET. Can someone help to translate this PHP code in ASP.NET?
All-Star
42975 Points
7025 Posts
MVP
Feb 12, 2013 05:24 PM|LINK
Hello,
mitkomk I am trying do do same as PHP with ASP.NET. Can someone help to translate this PHP code in ASP.NET?
You can make use of PHP to ASP.NET Migration Assitant.
Please 'Mark as Answer' if this post helps you.
mitkomk
Member
129 Points
36 Posts
Handle POST operation with img/jpeg content from Java
Feb 08, 2013 07:08 AM|LINK
Hi,
I work with Morena 6 scan java library and must to Handle POST operation with img/jpeg content: Here how to send data:
private class UploadImageAction extends AbstractAction implements Runnable { UploadImageAction() { super("upload to server"); } public void actionPerformed(ActionEvent event) { new Thread(this).start(); } public synchronized void run() { try { status.setText("Working ..."); Image image=selected.getImage(); ByteArrayOutputStream tmp=new ByteArrayOutputStream(); BufferedImage bufferedImage=new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); bufferedImage.createGraphics().drawImage(image, 0, 0, null); ImageIO.write(bufferedImage, "jpg", tmp); tmp.close(); int contentLength=tmp.size(); if (contentLength>1024*1024) throw new Exception("Image is too big to upload"); URL uploadURL=new URL(documentBase, "upload.php"); HttpURLConnection connection=(HttpURLConnection)uploadURL.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setUseCaches(false); connection.setDefaultUseCaches(false); connection.setRequestProperty("content-type", "img/jpeg"); connection.setRequestProperty("content-length", String.valueOf(contentLength)); OutputStream out=connection.getOutputStream(); out.write(tmp.toByteArray()); out.close(); InputStream in=connection.getInputStream(); int c; while ((c=in.read())!=-1) System.err.write(c); in.close(); URL imageURL=new URL(documentBase, connection.getHeaderField("file-name")); status.setText("Done - image is uploaded to "+imageURL+" (for at least 5 minutes) ..."); } catch (Throwable exception) { JOptionPane.showMessageDialog(MainPanel.this, exception.toString(), "Error", JOptionPane.ERROR_MESSAGE); exception.printStackTrace(); status.setText("Failed, try again ..."); } } public boolean isEnabled() { return hasServer && selected != null; } }in PHP it is done by:
<?PHP $dh = opendir("."); while (false !== ($filename = readdir($dh))) { $dc[] = $filename; } foreach($dc as $f) { if (substr($f, 0, 4)=="img_") if (filectime($f)<time()-5*60) unlink($f); } $fn="img_".time().".jpg"; $fp=fopen($fn,"w"); fwrite($fp, $HTTP_RAW_POST_DATA); fclose($fp); header("file-name: ".$fn); ?>I am trying do do same as PHP with ASP.NET. Can someone help to translate this PHP code in ASP.NET?
Ruchira
All-Star
42975 Points
7025 Posts
MVP
Re: Handle POST operation with img/jpeg content from Java
Feb 12, 2013 05:24 PM|LINK
Hello,
You can make use of PHP to ASP.NET Migration Assitant.
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.