标签:
package com.isoftstone.eply.servlet; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StringReader; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.UUID; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; 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.isoftstone.eply.util.ChangeFileToString; import com.isoftstone.eply.util.CheckElectronicPolicy; import com.isoftstone.eply.util.CreateCheckXml; import com.isoftstone.eply.vo.EPlyResponseFileVo; import com.isoftstone.eply.vo.EPlyResponseVo; import com.isoftstone.fwk.util.SpringUtils; public class ElectroicPolicyUploadFileServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { //得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全 String savePath = this.getServletContext().getContextPath()+"/upload"; File file = new File(savePath); //判断上传文件的保存目录是否存在 if (!file.exists() && !file.isDirectory()) { System.out.println(savePath+"目录不存在,需要创建"); //创建目录 file.mkdir(); } String message = "";//消息提示 try{ //使用Apache文件上传组件处理文件上传步骤: //1、创建一个DiskFileItemFactory工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); //2、创建一个文件上传解析器 ServletFileUpload upload = new ServletFileUpload(factory); //解决上传文件名的中文乱码 upload.setHeaderEncoding("UTF-8"); //3、判断提交上来的数据是否是上传表单的数据 if(!ServletFileUpload.isMultipartContent(request)){ //按照传统方式获取数据 return ; } //4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>集合,每一个FileItem对应一个Form表单的输入项 List<FileItem> list = upload.parseRequest(request); for(FileItem item : list){ //如果fileitem中封装的是普通输入项的数据 if(item.isFormField()){ String name = item.getFieldName(); //解决普通输入项的数据的中文乱码问题 String value = item.getString("UTF-8"); //value = new String(value.getBytes("iso8859-1"),"UTF-8"); System.out.println("普通输入项的名称和数据"+name + "=" + value); CPlyNo=value; }else{//如果fileitem中封装的是上传文件 //得到上传的文件名称, String filename = item.getName();if(filename==null || filename.trim().equals("")){ continue; } //注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如: c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt //处理获取到的上传文件的文件名的路径部分,只保留文件名部分 filename = filename.substring(filename.lastIndexOf("\\")+1); //获取item中的上传文件filename的输入流 InputStream in = item.getInputStream(); //创建一个文件输出流 FileOutputStream out = new FileOutputStream(savePath + "/" + filename); //创建一个缓冲区 byte buffer[] = new byte[1024]; //判断输入流中的数据是否已经读完的标识 int len = 0; //循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据 while((len=in.read(buffer))>0){ //使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中 out.write(buffer, 0, len); } //关闭输入流 in.close(); //关闭输出流 out.close(); //删除处理文件上传时生成的临时文件 item.delete(); UUID uuid = UUID.randomUUID(); // 得到对象产生的ID String resid = uuid.toString();
String xmldata= "<?xml version=\"1.0\" encoding=\"GBK\"?><PACKET><HEAD><KEY>E03</KEY><BUSINESS_SOURCE>0</BUSINESS_SOURCE><NUM>1100101000108000320150000092</NUM><TYPE>A</TYPE></HEAD></PACKET>";//拼报文 File xmlFile= new File(savePath +"/"+resid+".xml");//保存xml格式文件 if (!xmlFile.getParentFile().exists()) xmlFile.getParentFile().mkdirs(); BufferedReader bufferedReader = new BufferedReader(new StringReader(xmldata)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(xmlFile)); char buf[] = new char[1024]; //字符缓冲区 int len2; while ((len2 = bufferedReader.read(buf)) != -1) { bufferedWriter.write(buf, 0, len2); } bufferedWriter.flush(); bufferedReader.close(); bufferedWriter.close(); DataSource source = new FileDataSource(xmlFile); DataHandler dataHandler =new DataHandler(source); EPlyResponseVo epresponse=new EPlyResponseVo(); List<EPlyResponseFileVo> listvo = new ArrayList<EPlyResponseFileVo>(); File file=new File(savePath +"/"+filename); DataSource source2 = new FileDataSource(file); DataHandler dataHandler2 =new DataHandler(source2); xmlFile.delete(); //删除临时文件 filepdf.delete(); //删除临时文件 }catch (Exception e) { if("".equals(message)){ message = "上传文件失败,请重新上传!"; } e.printStackTrace(); } response.setContentType("text/html;charset=gb2312"); PrintWriter out=response.getWriter(); out.println("<script type=‘text/javascript‘>alert(‘"+message+"‘)</script>"); out.print("<script language=javascript>window.close();"); out.print("</script>"); out.close(); } }
Servlet 实现上传文件以及同时,写入xml格式文件和上传
标签:
原文地址:http://www.cnblogs.com/joyblabla/p/4819815.html