日期:2014-05-18 浏览次数:20739 次
public class getImage extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK"; public void init(ServletConfig config) throws ServletException { super.init(config); } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); Tool tool = new Tool(); ServletInputStream inStream = request.getInputStream(); //获取上传文件流 int streamLenth = request.getContentLength(); //获取上传文件的长度 //将文件存入临时文件中 FileOutputStream tmpfileStream = null; String tmpfileName = null; tmpfileName = "c:\\temp\\" + request.getSession().getId(); tmpfileStream = new FileOutputStream(tmpfileName); int bytesRead = 0, totalBytes = 0; byte[] cnt; while (totalBytes < streamLenth) { cnt = new byte[256]; bytesRead = inStream.read(cnt, 0, 256); totalBytes += bytesRead; tmpfileStream.write(cnt); } tmpfileStream.close(); inStream.close(); //现已将Http协议上传的内容全部写入了临时文件。以下的代码便可在服务器上对上传的内容进行解释 //以随机读写方式打开http临时流文件。 // RandomAccessFile(String name, String mode) - 类 java.io.RandomAccessFile 的构造方法 // 创建从中读取和向其中写入(可选)的随机存取文件流,该文件具有指定名称。 // name - 取决于系统的文件名 mode - 此存取 mode long cntStartPoint = 0, cntEndPoint = 0; RandomAccessFile randomFile = null; randomFile = new RandomAccessFile(tmpfileName, "rw"); cntStartPoint = randomFile.getFilePointer(); String sgmtDeli = randomFile.readLine(); //out.println(sgmtDeli); String line=null; boolean eofblock = false; while (!eofblock) { line = randomFile.readLine(); //out.println(line+"<br />"); if(line.equals(sgmtDeli)){ out.println(line+"<br />"); } if (line.contains(sgmtDeli+"--")) eofblock = true; } out.close(); } // public String cntEndPoint(String line,String sgmtDeli){ // String bonuy= // } }
package yuanyifileup; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.PageContext; public class yuanyifileup { private ServletRequest request; private ServletResponse response; private ServletConfig config; ServletInputStream DATA; int FormSize; File f1; FileOutputStream os; DataInputStream is; String filename; byte[] b; byte t; bool