这是代码,它将参数fname,selectedFile(byteArray)作为发布请求主体的一部分从android发送到Tomcat服务器。
public void uploadFile(){
EditText filename=findViewById(R.id.fileName);
//content://
try {
InputStream inputStream=getContentResolver().openInputStream(filePath);
selectedFile= IOUtils.toByteArray(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String purl="http://192.168.1.11:8080/placement/V2TryUploadFile";
RequestBody requestBody=new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("fname",filename.getText().toString())
.addFormDataPart("selectedFile",filename.getText().toString(),RequestBody.create(MediaType.parse("application/pdf"),selectedFile))
.build();
Request request=new Request.Builder()
.url(purl)
.post(requestBody)
.build();
Toast.makeText(MainActivity.this,filename.getText().toString(),Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this,selectedFile.length+":",Toast.LENGTH_SHORT).show();
OkHttpClient okHttpClient=new OkHttpClient();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
call.cancel();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"Failed to Upload",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String responseString=response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,responseString,Toast.LENGTH_SHORT).show();
}
});
}
});
Servlet代码(在tomcat服务器上运行)以处理从android add发送的发布请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String fname=request.getParameter("fname")+".pdf";
Part selectedFile=request.getPart("selectedFile");
System.out.println(fname);
System.out.println(selectedFile+"");
InputStream filecontent = selectedFile.getInputStream();
byte[] fileAsByteArray = IOUtils.toByteArray(filecontent);
FileOutputStream imageOutFile = new FileOutputStream("C:\\Users\\VAISHAK`\\placeWorkSpace\\placement\\ResumeUploads\\" + fname);
imageOutFile.write(fileAsByteArray);
imageOutFile.close();
doGet(request, response);
}
问题是fname和selectedFile在android应用程序中未显示为null,但在tomcat服务器控制台中都显示为null。
也下面是Eclipse中的tomcat服务器登录
null.pdf
null
Apr 13, 2020 11:56:20 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [trial.V2TryUploadFile] in context with path [/placement] threw exception
java.lang.NullPointerException
at trial.V2TryUploadFile.doPost(V2TryUploadFile.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:615)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1623)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
我终于找到了解决方案
默认情况下,Servlet不处理
MutipartBody FORM
因此,为了支持它,我们需要将以下注释添加到servlet中
@MultipartConfig