java – 例1

最后更新于:2022-04-01 23:42:23

**以下代码仅作为参考示例,不建议直接拷贝到项目中使用** ---- java 代码示例 ```java import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import com.sun.xml.internal.ws.wsdl.writer.document.Part; public class UploadImgServiet extends HttpServlet { public UploadImgServiet() { super(); } public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getRealPath("/image"); File file = new File(path); if (!file.exists()) file.mkdirs(); String fileName = "";// 文件名称 /**上传文件处理内容**/ DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload sfu = new ServletFileUpload(factory); sfu.setHeaderEncoding("UTF-8"); // 处理中文问题 sfu.setSizeMax(1024 * 1024); // 限制文件大小 try { List fileItems = sfu.parseRequest(request); // 解码请求 for (FileItem fi : fileItems) { fileName = UUID.randomUUID()+fi.getName().substring(fi.getName().lastIndexOf("."),fi.getName().length()); fi.write(new File(path, fileName)); } } catch (Exception e) { e.printStackTrace(); } /**********************/ //获取图片url地址 string imgUrl = "http://localhost:8080/wang_editor_demo/image/" + fileName; response.setContentType("text/text;charset=utf-8"); PrintWriter out = response.getWriter(); out.print(imgUrl); //返回url地址 out.flush(); out.close(); } public void init() throws ServletException { // Put your code here } } ```
';