In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The content of this article is mainly about how to upload java web files, the content of the article is clear and clear, it is very suitable for beginners to learn, it is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
Java web file upload file resource location: HTML page under web/img to learn file download using hyperlink Picture 1 Video 1 Picture 2 Picture 1 Video 1 Chinese Picture 2Servlet Class @ WebServlet ("/ downServlet") public class FileDownServlet extends HttpServlet {protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / get request parameters File name String filename = request.getParameter ("filename") ServletContext context = this.getServletContext (); / / find the real path to the server file String realPath = context.getRealPath ("/ img/" + filename); / / use byte stream to associate FileInputStream fis=new FileInputStream (realPath) / * set response header type: content-type response header opening method: content-disposition * / String mimeType = context.getMimeType (filename); response.setHeader ("content-type", mimeType); / / get user-agent request header String agent = request.getHeader ("user-agent") / / use the tool class method to encode the file name filename= DownLoadUtils.getFileName (agent, filename); response.setHeader ("content-disposition", "attachment;filename=" + filename); / / write the data of the input stream to the output stream ServletOutputStream sos = response.getOutputStream (); byte [] bytes=new byte [1024x8]; int len=0 While ((len=fis.read (bytes))! =-1) {sos.write (bytes,0,len);} fis.close ();} protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost (request,response) }} util utility class: encode public class DownLoadUtils {public static String getFileName (String agent, String filename) throws UnsupportedEncodingException {if (agent.contains ("MSIE")) {/ / IE browser filename = URLEncoder.encode (filename, "utf-8"); filename = filename.replace ("+", "") } else if (agent.contains ("Firefox")) {/ / Firefox BASE64Encoder base64Encoder = new BASE64Encoder (); filename = "? utf-8?B?" + base64Encoder.encode (filename.getBytes ("utf-8")) + "? =";} else {/ / other browsers filename = URLEncoder.encode (filename, "utf-8") } return filename;}} what are the characteristics of Java? what are the characteristics of Java? what is the 1.Java language that represents the static object-oriented programming language? it implements the object-oriented theory and allows programmers to carry out complex programming in an elegant way of thinking. 2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, dynamic and so on. 3. Using Java, you can write desktop applications, Web applications, distributed systems, embedded system applications, and so on.
Thank you for your reading, I believe you have a certain understanding of "how to upload java web files", go to practice quickly, if you want to know more related knowledge points, you can follow the website! The editor will continue to bring you better articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.