0
回答
这报的是什么错误啊 servlet io
注册华为云得mate10,2.9折抢先购!>>>   
package ban;  源码

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: UploadServlet
 * 
 */
public class UploadServlet extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	
	public static final String firstUpload = "FirstUpload";

	// 存放上传过程中文件信息

	private static final String FILE_ID = "T23434AB"; // 随便定义的

	private static final String FILE_PATH = "f://temp";
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		
		RandomAccessFile file = null; // 文件写入
		InputStream is = null; // request 的输入流
		DataInputStream dis = null; // request的装配流
		OutputStream os = null;// response的输出流
		DataOutputStream dos = null; // response的装配流
		try {
			is = request.getInputStream();
			System.err.println("dfdfdfdfdfdfd"+is.toString());
			dis = new DataInputStream(is);
			os = response.getOutputStream();
			dos = new DataOutputStream(os);

		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		byte actId = 0; // 提交过来的操作id,1为刚上传,2为续传,3为结束通知
		String fileId = ""; // 提交过来的操作文件id
		long writePos = 0L; // 开始写入地址

		try {
			// 读取操作标识及其它基本信息
			actId = dis.readByte();
			fileId = dis.readUTF();
			writePos = dis.readLong();
			// 对读取的信息处理

			if (actId == 1 && fileId.equals("")) {
				// 如果是开始上传操作
				// 在对应的位置创建文件,并把文件路径放入Map
				// 将传入的流里面的数据写入到指定文件中
				// 写入完成后,把写入的进度写入数据库
				// 返回文件的唯一码
				file = new RandomAccessFile(new File(FILE_PATH), "rw");
				file.seek(writePos);
				byte[] tmp = new byte[1024 * 1024];
				int len = 0;
				while ((len = is.read(tmp)) != -1) {
					file.write(tmp, 0, len);
				}
				is.close();
				dis.close();
				dos.writeByte(3);
				dos.writeUTF(FILE_ID);
				dos.writeByte(1);
				os.flush();
				os.close();
			} else if (actId == 2) {
				// 如果是续传操作 ×××其实与上面情况一样××××代码还没有优化。。
				// 从对应的路径里面调出文件
				// 从数据库读取写入进度
				// 将流写入到文件中
				file = new RandomAccessFile(new File(FILE_PATH), "rw");
				file.seek(writePos);
				byte[] tmp = new byte[1024 * 1024];
				int len = 0;
				while ((len = is.read(tmp)) != -1) {
					file.write(tmp, 0, len);
				}
				is.close();
				dis.close();
				dos.writeByte(3);
				dos.writeUTF(FILE_ID);
				dos.writeByte(1);
				os.flush();
				os.close();
			} else if (actId == 3) {
				// 如果是文件完成通知
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null)
					is.close();
				if (file != null)
					file.close();
				if (os != null)
					os.close();
				if (dos != null)
					dos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		System.out.println("tran end");
	}
	
}
java.io.EOFException at java.io.DataInputStream.readFully(DataInputStream.java:197) at java.io.DataInputStream.readUTF(DataInputStream.java:609) at java.io.DataInputStream.readUTF(DataInputStream.java:564) tran end at ban.UploadServlet.doPost(UploadServlet.java:63) at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722)
举报
帅狗
发帖于4年前 0回/191阅
顶部