2013年9月5日星期四

android upload pictures backstage at a null pointer exception

I first talk about the problems encountered : In the android client upload pictures backstage at a null pointer exception userImage this object is null

server segment 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;

}


upload pictures using JSP server is running correctly can also see pictures uploaded to the background

android client code :
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 */


}

My actionURL:
private String actionUrl = "http://192.168.2.100:8888/ImportedGood/userImage.action";


I do not know is the client code issues have not done before seeking experience sharing . .
------ Solution ---------------------------------------- ----
you upload files splicing code header files it issues
------ reference -------------------- -------------------
man did not understand what you mean I'm not an example of online reference written to the output stream directly to the picture inside it

没有评论:

发表评论