我正在开发一个项目,可以将文件从本地PC网络浏览器(Chrome或IE)上传到手机网络服务器。
我有些如何设法将文件上传到服务器,代码就像这样
$.ajax({
url: loc,
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res)
{
$.prompt("File uploaded",
{
buttons :
{
"OK" : true
},
callback : refreshFileCallback
});
}
});
但是
如何在java中通过POST发送上传的文件?
网络服务器代码
package org.join.wfs.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.DefaultHttpServerConnection;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.HttpRequestHandlerRegistry;
import org.apache.http.protocol.HttpService;
import org.apache.http.protocol.ResponseConnControl;
import org.apache.http.protocol.ResponseContent;
import org.apache.http.protocol.ResponseDate;
import org.apache.http.protocol.ResponseServer;
public class WebServer extends Thread
{
static final String SUFFIX_ZIP = "..zip";
static final String SUFFIX_DEL = "..del";
static final String SUFFIX_REN = "..ren";
static final String SUFFIX_NEW_FOLDER = "..new";
static final String SUFFIX_CREATE_FILE = "..cre";
static final String SUFFIX_MORE = "..mor";
static final String SUFFIX_UPLOAD = "..upl";
private int port;
private String webRoot;
private boolean isLoop = false;
public WebServer(int port, final String webRoot)
{
super();
this.port = port;
this.webRoot = webRoot;
}
@Override
public void run()
{
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(port);
BasicHttpProcessor httpproc = new BasicHttpProcessor();
httpproc.addInterceptor(new ResponseDate());
httpproc.addInterceptor(new ResponseServer());
httpproc.addInterceptor(new ResponseContent());
httpproc.addInterceptor(new ResponseConnControl());
HttpService httpService = new HttpService(httpproc, new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory());
HttpParams params = new BasicHttpParams();
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE,8 * 1024)
.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
.setParameter(CoreProtocolPNames.ORIGIN_SERVER,"WebServer/1.1");
httpService.setParams(params);
HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
reqistry.register("*" + SUFFIX_ZIP, new HttpZipHandler(webRoot));
reqistry.register("*" + SUFFIX_DEL, new HttpDelHandler(webRoot));
reqistry.register("*" + SUFFIX_REN, new HttpRenameHandler(webRoot));
reqistry.register("*" + SUFFIX_NEW_FOLDER, new HttpNewFolderHandler(webRoot));
reqistry.register("*" + SUFFIX_CREATE_FILE, new HttpCreateFileHandler(webRoot));
reqistry.register("*" + SUFFIX_MORE, new HttpMoreHandler(webRoot));
reqistry.register("*" + SUFFIX_UPLOAD, new HttpUploadFileHandler(webRoot));
reqistry.register("*", new HttpFileHandler(webRoot));
httpService.setHandlerResolver(reqistry);
isLoop = true;
while (isLoop && !Thread.interrupted())
{
Socket socket = serverSocket.accept();
DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
conn.bind(socket, params);
Thread t = new WorkerThread(httpService, conn);
t.setDaemon(true);
t.start();
}
}
catch (IOException e)
{
isLoop = false;
e.printStackTrace();
}
finally
{
try
{
if (serverSocket != null)
{
serverSocket.close();
}
}
catch (IOException e)
{
}
}
}
public void close()
{
isLoop = false;
}
}
如果不可能,请建议我另一种方法,如何将文件从HTML上传到java
三江源