-
-
Save dojo-softeam/1234092 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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