Skip to content

Instantly share code, notes, and snippets.

@dojo-softeam
Forked from jeantil/BinText.java
Created September 22, 2011 05:09
Show Gist options
  • Save dojo-softeam/1234092 to your computer and use it in GitHub Desktop.
Save dojo-softeam/1234092 to your computer and use it in GitHub Desktop.
package eu.byjean.bintext;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.repackaged.com.google.common.util.Base64;
@SuppressWarnings("serial")
public class BintextServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
try {
URL url = new URL(req.getParameter("url"));
HttpURLConnection co = (HttpURLConnection)url.openConnection();
co.setInstanceFollowRedirects(true);
co.setConnectTimeout(0);
co.setReadTimeout(0);
InputStream is = co.getInputStream();
byte[] buffer = new byte[10240];
while(is.read(buffer)>0){
resp.getWriter().print(Base64.encode(buffer));
resp.flushBuffer();
}
is.close();
} catch (MalformedURLException e) {
resp.getWriter().println("Malformed url: "+e.getMessage());
} catch (IOException e) {
resp.getWriter().println("IOEXception :"+e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment