我在后端使用Spring MVC实现文件上传,前端只是一个普通的HTML
<form method="POST" enctype="multipart/form-data" action="/upload">
file to upload: <input type="file" name="file"><br>
<input type="submit" value="Upload" />
</form>
Run Code Online (Sandbox Code Playgroud)
一切正常,除非我在上传过程中关闭浏览器,否则我会看到服务器抛出错误(您必须尽早中断该过程才能看到它)
java.io.EOFException: Unexpected EOF read on the socket
at org.apache.coyote.http11.InternalNioInputBuffer.fill(InternalNioInputBuffer.java:152)
at org.apache.coyote.http11.InternalNioInputBuffer$SocketInputBuffer.doRead(InternalNioInputBuffer.java:177)
at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:110)
at org.apache.coyote.http11.AbstractInputBuffer.doRead(AbstractInputBuffer.java:416)
at org.apache.coyote.Request.doRead(Request.java:460)
at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:338)
at org.apache.tomcat.util.buf.ByteChunk.substract(ByteChunk.java:395)
...
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更好的方法来处理异常,或者我可以安全地保持原样?
在包含的测试程序中,我尝试将文件从本地磁盘复制到HDFS.代码如下:
package foo.foo1.foo2.test;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class TestTestTest {
public static void main(String[] args) {
String srcLocation = "foo";
String destination = "hdfs:///tmp/";
FileSystem hdfs = null;
Configuration configuration = new Configuration();
configuration.set("fs.default.name", "hdfs://namenode:54310/");
try {
hdfs = FileSystem.get(configuration);
} catch (IOException e2) {
e2.printStackTrace();
return;
}
Path srcpath = new Path(srcLocation);
Path dstpath = new Path(destination);
try {
hdfs.copyFromLocalFile(srcpath, dstpath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这失败,出现以下异常:
java.io.IOException: Call to …
Run Code Online (Sandbox Code Playgroud) 我不知道为什么会出现java.io.EOFException.我想从服务器获取二进制流后写一个文件.
这是我的代码
inputStream = new DataInputStream(new BufferedInputStream(connection.getInputStream()));
FileOutputStream fos = new FileOutputStream("D:/Apendo API resumable download.txt");
byte b = inputStream.readByte();
while(b != -1){
fos.write(b);
b = inputStream.readByte();
}
fos.close();
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪
java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at HttpRequestJSON.JSONRequest.sendRequest(JSONRequest.java:64)
at HttpRequestJSON.Main.main(Main.java:56)
Run Code Online (Sandbox Code Playgroud) 我EOFException
在使用readUTF()
方法时遇到问题,请告诉我如何解决此问题,并建议如何readUTF()
在其他网络上传输套接字信息
import java.io.*;
import java.net.*;
public class GreetingServer {
public static void main(String args[]) {
String servername =args[0];
int port = Integer.parseInt(args[1]);
try {
System.out.println("Server Name "+ servername +"Port"+port);
Socket client = new Socket(servername,port);
System.out.println("Just connected to"+ client.getRemoteSocketAddress());
OutputStream outs = client.getOutputStream();
DataOutputStream dout = new DataOutputStream(outs);
dout.writeUTF("Hello From"+client.getRemoteSocketAddress());
InputStream in = client.getInputStream();
DataInputStream din = new DataInputStream(in);
System.out.println("Server Says"+ din.readUTF());
client.close();
}
catch (EOFException f) {
f.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
} …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Java websocket,但收到一个我不明白的错误。
当服务器获取 aRuntimeException
或NullPointerException
等时,onError
将调用该方法。
之后(完成 onError 方法),onError
再次调用该方法。第二次之后,EOFException
连续发生。
您知道为什么会在方法EOFException
之后发生吗?onError
这是我的错误日志:
java.io.EOFException
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1267)
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.isReadyForRead(NioEndpoint.java:1176)
at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:58)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.upgradeDispatch(WsHttpUpgradeHandler.java:148)
at org.apache.coyote.http11.upgrade.UpgradeProcessorInternal.dispatch(UpgradeProcessorInternal.java:54)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:788)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1485)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Run Code Online (Sandbox Code Playgroud) 我在applet端有以下代码:
URL servlet = new URL(appletCodeBase, "FormsServlet?form=requestRoom");
URLConnection con = servlet.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/octet-stream");
ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
out.writeObject(user);//user is an object of a serializable class
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(con.getInputStream());
status = (String)in.readObject();
in.close();
if("success".equals("status")) {
JOptionPane.showMessageDialog(rootPane, "Request submitted successfully.");
} else {
JOptionPane.showMessageDialog(rootPane, "ERROR! Request cannot be made at this
time");
}
Run Code Online (Sandbox Code Playgroud)
在servlet方面,我收到如下代码:
form = request.getParameter("form");
if("requestRoom".equals(form)) {
String fullName, eID, reason;
UserRequestingRoom user;
try {
in = new ObjectInputStream(request.getInputStream());
user = (UserRequestingRoom)in.readObject(); …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个使用对象输出和输入流的应用程序.但是我有一个问题,因为我无法正确发送我的对象.我将它写入流,并且服务器给我一个类未找到异常,即使客户端和服务器都具有完全相同的此类副本(唯一的区别是包名称)具有相同的序列ID.这是我的班级:
import java.io.Serializable;
public class Message implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String username = null;
private String hashedPassword = null;
private Integer code = null;
private String from = null;
private String to = null;
private Object data = null;
public Message() {
}
public Message(Integer code) {
this.code = code;
}
public Message(Integer code, Object data) {
this.code = code;
this.data = data;
}
public Message(String username, String hashedPassword, Integer …
Run Code Online (Sandbox Code Playgroud) 我对编程完全陌生,所以我很难解决自己的错误。有人建议我在这个网站上尝试一下,所以我想为什么不试一试。
我发现的有关此错误的其他帖子似乎不太相关:大多数人建议关闭输入流,但我的代码已经这样做了。
我想要它做什么:将一个名为“photo”的 Photo 对象写入一个名为“test.ser”的文件中。然后读取文件“test.ser”并将“test.ser”中对象(“photo”)的路径返回给我。
它的实际作用:将一个名为“photo”的 Photo 对象写入“test.ser”。读取“test.ser”,返回一个 EOFException 并且没有路径。
返回路径实际上并不是很重要,只要它返回对我有价值的东西即可。但是当我使用“System.out.println(photo)”或“photo.getId()”时,我遇到了同样的错误。
我不太确定我需要在这里粘贴什么,所以我将发布用于序列化和反序列化对象的两个 try/catch-es:
序列化对象:
File test = new File("path.../something.ser");
Photo photo = new Photo(2, "..\\images\\2.jpg", getImage("..\\images\\2.jpg"));
try {
FileOutputStream fos = new FileOutputStream(test);
ObjectOutputStream out = new ObjectOutputStream(fos);
if (!test.exists()) {
test.createNewFile();
}
out.writeObject(photo);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
反序列化对象:
try {
FileInputStream fis = new FileInputStream(test);
ObjectInputStream in = new ObjectInputStream(fis);
in.readObject();
photo = (Photo)in.readObject();
photo.getPath();
in.close();
} catch (Exception e) {
e.printStackTrace();
} …
Run Code Online (Sandbox Code Playgroud) 03-26 14:12:19.045: E/Webservices(2863): java.io.EOFException
03-26 14:12:19.045: E/Webservices(2863): at libcore.io.Streams.readAsciiLine(Streams.java:203)
03-26 14:12:19.045: E/Webservices(2863): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
03-26 14:12:19.045: E/Webservices(2863): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
03-26 14:12:19.045: E/Webservices(2863): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
03-26 14:12:19.045: E/Webservices(2863): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
03-26 14:12:19.045: E/Webservices(2863): at org.ksoap2.transport.ServiceConnectionSE.getResponseCode(ServiceConnectionSE.java:103)
03-26 14:12:19.045: E/Webservices(2863): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:197)
03-26 14:12:19.045: E/Webservices(2863): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
03-26 14:12:19.045: E/Webservices(2863): at .utils.Webservices.callinternet(Webservices.java:125)
03-26 14:12:19.045: E/Webservices(2863): at .utils.Webservices.getResponse(Webservices.java:73)
03-26 14:12:19.045: E/Webservices(2863): at .utils.Webservices.getResponse(Webservices.java:79)
03-26 14:12:19.045: E/Webservices(2863): at .utils.Webservices.getResponse(Webservices.java:79)
03-26 14:12:19.045: E/Webservices(2863): at .utils.AsynTask.doInBackground(AsynTask.java:61)
03-26 14:12:19.045: E/Webservices(2863): at .utils.AsynTask.doInBackground(AsynTask.java:1)
03-26 14:12:19.045: E/Webservices(2863): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-26 …
Run Code Online (Sandbox Code Playgroud) 我编写了一个简单的服务器来监听客户端,当客户端连接时,它打开一个Datainputstream,读取客户端发送的所有数据(我的客户端的UTF数据).
这是ServerCode:
@Override
public void run() {
// TODO Auto-generated method stub
try {
ServerSocket ss = new ServerSocket(7000);
while(true){
System.out.println("Il Server sta cercando Connessioni");
Socket s = ss.accept();
System.out.println("Il Server ha accettato un Client");
Thread t2 = new Thread(new Runnable(){
public void run(){
try {
while(true){
DataInputStream dis = new DataInputStream(s.getInputStream());
isAlreadyOpened = true;
System.out.println(dis.readUTF());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
isAlreadyOpened = false;
}
}
});
if(!isAlreadyOpened){
t2.start();
}
}
} catch …
Run Code Online (Sandbox Code Playgroud) 我想读取我输出到.dat文件的多个对象(我自己的类Term),但我总是得到一个nullPointException或EOFException.
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(masterFile));
Object o = null;
while(( o = inputStream.readObject()) != null){
Term t = (Term)o;
System.out.println("I found a term");
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个程序,用户可以:1)向联系人添加人员(姓名,电话,电子邮件),2)从联系人中删除一个人,3)从联系人中读取所有人.
我这样做的方式是我要求用户选择并分别做任何事情.对于写入,我只是将一个对象写入该文件.为了删除,我想我会问用户"姓"将用作KEY(因为我正在使用TreeMap)并将删除键上的值(对象).
所以我在这里读书时遇到了问题.我试图像这样读取对象:
public void readContact()
{
TreeMap<String, Contact> contactMap = new TreeMap<String, Contact>();
try
{
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(
new FileInputStream(file)));
while( in.available() > 0 ) //This line does NOT read
{
Contact c = (Contact)in.readObject();
contactMap.put(c.getLastName(), c);
}
for(Map.Entry contact : contactMap.entrySet() )
{
Contact con = contactMap.get( contact.getKey() );
System.out.println( con.getLastName() + ", " + con.getFirstName() + ": " + con.getPhoneNumber() + "\t" + con.getEmail());
}
}
catch(Exception e)
{
System.out.println("Exception caught");
}
}
Run Code Online (Sandbox Code Playgroud)
请不要建议做 …
我想要做的是将HashMap写入文件.一次运行时,下面的代码正确.但是,当我尝试运行只是将对象写入文件并尝试只是单独运行读取写入的对象时,它会抛出以下异常.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
public class FileIo {
String fileName;
FileOutputStream fos;
FileInputStream fis;
ObjectOutputStream oos;
ObjectInputStream ois;
public FileIo(String fileName) {
this.fileName = fileName;
File file = new File(fileName);
try {
if (!file.exists()) {
file.createNewFile();
System.out.println("created");
}
fos = new FileOutputStream(fileName);
fis = new FileInputStream(fileName);
oos = new ObjectOutputStream(fos);
ois = new ObjectInputStream(fis);
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeToFile(Object obj) { …
Run Code Online (Sandbox Code Playgroud) eofexception ×13
java ×13
android ×1
exception ×1
file ×1
file-upload ×1
generics ×1
hadoop ×1
hdfs ×1
io ×1
ksoap2 ×1
spring-mvc ×1
treemap ×1
websocket ×1