先说说我碰到的问题:在android客户端上传图片后 后台报空指针异常  userImage这对象为空服务器段demo:
private File userImage;
private String userImageContentType;
private String userImageFileName; private HttpServletRequest servletRequest; public String execute() {
try {


String filePath = servletRequest.getSession().getServletContext()
.getRealPath("/Image/img_logo/");
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, this.userImageFileName);
FileUtils.copyFile(this.userImage, fileToCreate);
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
return INPUT;
}
return SUCCESS;
} public File getUserImage() {
return userImage;
} public void setUserImage(File userImage) {
this.userImage = userImage;
} public String getUserImageContentType() {
return userImageContentType;
} public void setUserImageContentType(String userImageContentType) {
this.userImageContentType = userImageContentType;
} public String getUserImageFileName() {
return userImageFileName;
} public void setUserImageFileName(String userImageFileName) {
this.userImageFileName = userImageFileName;
} public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest; }使用JSP上传图片服务器端是能正常运行的也能看到图片上传到后台android客户端代码:
private void uploadFile() {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****"; URL url = null;
try {
url = new URL(actionUrl);
} catch (MalformedURLException e) { e.printStackTrace();
}
HttpURLConnection con = null;
try {
con = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
try {
con.setRequestMethod("POST");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary="
+ boundary);
/* 设置DataOutputStream */
DataOutputStream ds;
try {
ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; "
+ "name=\"userImage\";filename=\"" + newName + "\"" + end);
ds.writeBytes(end);
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFilePath);
/* 设置每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
/* 从文件读取数据至缓冲区 */
while ((length = fStream.read(buffer)) != -1) {
/* 将资料写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
/* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
ds.close();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "上传成功", Toast.LENGTH_SHORT)
.show();
} });
} catch (IOException e) {
e.printStackTrace();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "上传失败", Toast.LENGTH_SHORT)
.show();
} });
} /* 将Response显示于Dialog */
// showDialog("上传成功"+b.toString().trim());
/* 关闭DataOutputStream */
}
我的actionURL:private String actionUrl = "http://192.168.2.100:8888/ImportedGood/userImage.action";我不知道是客户端代码问题 之前没做过 求经验分享。。