WebClient uploads file JSP to accept file stream data

Source: Internet
Author: User

The CS software sends data to the BS-side JSP page. jsp accepts the data and saves it as a file.

Below is the data sent by CSHARPCode:

 using (filestream = new filestream (file. fullname, filemode. open, fileaccess. read) {long Len = filestream. length; byte [] imgbytes = new byte [filestream. length]; filestream. read (imgbytes, 0, (INT) filestream. length); WebClient client = new WebClient (); string guid = guid. newguid (). tostring ("N"); string filesavepath = getfilesavepath (); // The path format is http: // 192.168. 0.201: 7001/affixfile/upload. jsp? Newfilename=aaa.jpg & filepath = c: \ SMZ \ 01 \ string posturl = string. format (@ "{01_1_12.16.jpg & filepath = {2} \ 0", appconfig. getvalue ("savephotourl"), guid, filesavepath); byte [] uploadbyte = client. uploaddata (posturl, "Post", imgbytes) ;}

The following is the code of the JSP terminal receiving data page:

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" %> <% @ page import = "Java. util. *, Java. io. *, Com. itsv. CMS. base. writer. publishfilewriter; "%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML lang = "true"> 

If the Asp.net page accepts data, you can use the following method:

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. io; namespace webckeditor {// <summary> // abstract description of imgupload /// </Summary> public class imgupload: ihttphandler {public void processrequest (httpcontext context) {// context. response. contenttype = "text/plain"; // context. response. write ("Hello World"); // app. log. info (context. request. URL. tostring (); // get the upload Data Stream string filenamestr = datetime. now. tostring ("yyyy-mm-ddhhmmssfff"); // context. request. querystring ["FILENAME"]; stream sr = context. request. inputstream; string newfilename = context. request. querystring ["newfilename"]; try {string filename = filenamestr; byte [] buffer = new byte [4096]; int bytesread = 0; // write the current data stream to the string targetpath = context in the clientbin folder on the server side. server. mappath ("~ /Uploadimg/"+ newfilename); Using (filestream FS = file. create (targetpath, 4096) {While (bytesread = sr. read (buffer, 0, buffer. length)> 0) {// write information to the file FS. write (buffer, 0, bytesread);} context. response. contenttype = "text/plain"; context. response. write ("uploaded successfully" + newfilename);} catch (exception e) {context. response. contenttype = "text/plain"; context. response. write ("Upload Failed, error message:" + E. message); // app. log. info (E. message);} finally {Sr. dispose () ;}} public bool isreusable {get {return false ;}}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.