Recently, in a project, you need to write a stream from httpurlconnection and get the stream object through request in struts. However, no matter what the operation is, the corresponding stream cannot be obtained in the struts request, it was depressing to say that the key point was found later, because the form submitted was set during stream writing, which caused a problem when obtaining the stream in struts, struts does some processing for the request with no Content-Type specified. As a result, the request cannot be obtained in the action. getinputstream () and request. getreader (). For details, see the code example.
1. The sendpost method gets the stream from the local device and writes it to the corresponding URL link:
(Important ):
// The parameter that must be added when the stream needs to be passed. This parameter must also be added when request. getinputstream is used in the action to obtain the stream.
Conn. setrequestproperty ("Content-Type", "text/html"); // directly transmits the Stream Object
// The following code uses the form component to transmit stream objects. You can view them online.
Conn. setrequestproperty ("Content-Type", "multipart/form-data; boundary =" + Java. util. UUID. randomuuid (). tostring ());
public static void main(String[] args) throws UnsupportedEncodingException { //String url = "http://61.154.14.46:8080/exter.shtml?serviceType=1011";String url = "http://localhost:8080/webtest/servlet/URLTest?name=linlin";//String url = "http://localhost:8081/exter.shtml?serviceType=1022&menuId=4481&mobile=15806092760&text_data=linlinlin&imgName=testa.jpg";//getReturnData1(url);sendPost(url,null);}
/*** Send the specified file to the specified URL * @ Param URL * @ throws ioexception */public static void sendpost (string URL, inputstream in) in the form of post through the HTTP protocol) {httpurlconnection conn = NULL; outputstreamwriter OSW = NULL; try {file = new file ("D:/test2.jpg"); If (! File. exists () {try {file. createnewfile ();} catch (ioexception e) {e. printstacktrace () ;}} URL url1 = new URL (URL); Conn = (httpurlconnection) url1.openconnection (); Conn. setreadtimeout (10000); // The Maximum Cache Time Conn. setdoinput (true); // allow the input of Conn. setdooutput (true); // allow the output of Conn. setusecaches (false); // cache Conn is not allowed. setrequestmethod ("Post"); Conn. setrequestproperty ("charsert", "UTF-8"); // Conn. setrequestproper Ty ("Content-Type", "multipart/form-data; boundary =" + Java. util. UUID. randomuuid (). tostring (); // the content that must be added when the stream needs to be passed, and the Action passes the request. this option conn must also be added for getinputstream obtaining. setrequestproperty ("Content-Type", "text/html"); outputstream o = Conn. getoutputstream (); bufferedinputstream Bis = new bufferedinputstream (New fileinputstream (File); int buffer_size = 1024; byte [] Buf = new byte [buffer_size]; int size = 0; try {While (size = bis. Read (BUF ))! =-1) O. write (BUF, 0, size);} catch (ioexception e) {e. printstacktrace ();} finally {try {bis. close (); O. close ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}} if (Conn. getresponsecode ()! = Httpurlconnection. http_ OK) system. Out. println ("Connect failed! ");} Catch (ioexception e) {e. printstacktrace ();} finally {If (OSW! = NULL) Try {OSW. Close ();} catch (ioexception E1) {e1.printstacktrace ();} If (Conn! = NULL) Conn. Disconnect ();}}
2. When writing a stream using the above method, you can get the corresponding stream information in the servlet or action. The Code is as follows:
Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html"); string S = request. getparameter ("name"); system. out. println ("s22 is" + S); inputstream in = request. getinputstream (); If (in! = NULL) {system. Out. println ("the stream is not empty. "); This. writeinputstreamtofile (in); system. out. println ("server time is" + new date ();} else {system. out. println ("the stream is empty. ");} Printwriter out = response. getwriter (); Out. println (" <! Doctype HTML public \ "-// W3C // dtd html 4.01 transitional // en \"> "); out. println ("<HTML>"); out. println ("
private void writeInputStreamToFile(InputStream in) throws FileNotFoundException {File file = new File("D:/test3.jpg");if(!file.exists()) {try {file.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} FileOutputStream fos = new FileOutputStream(file);BufferedInputStream bis = new BufferedInputStream(in);int BUFFER_SIZE = 1024; byte[] buf = new byte[BUFFER_SIZE]; int size = 0; try {while ((size = bis.read(buf)) != -1) fos.write(buf, 0, size);} catch (IOException e) {e.printStackTrace();} finally {try {bis.close();fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }