springmvc服务端+android客户端的文件上传

时间:2021-05-02 14:50:12

一、Springmvc服务端

1.引入文件上传的JAR包commons-fileupload-1.2.jar,commons-io-1.3.2.jar.

 

 

2. <!-- 配置上传的组件   用于SpringMvc查找commons包 -->

         <beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

</beans>

 

 

3.服务端

 

1)构建回传类

Result.java

ackage org.linfeng.action;
import org.springframework.stereotype.Component;

@Component
public class Result {
private String path;
private String code;
private String message;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}

2)构建控制器

@Controller

public class UploadController {

@Resource

privateResult result;



@RequestMapping("/mobile/uploadfile")

@ResponseBody //把回传类转换成json

public ResultuploadPhone(@RequestParam(value="file",required=false)MultipartFilefile,HttpServletRequest request) throws IllegalStateException, IOException{

Stringpath=uploadFile(file, request);

result.setCode("200");

result.setPath(path);

result.setMessage("上传成功");

returnresult;

}

/**

* 上传

* @param file

* @param request

* @return

* @throws IOException

*/

privateString uploadFile(MultipartFile file,HttpServletRequest request)

throwsIOException {

Stringpath=request.getSession().getServletContext().getRealPath("upload");

StringfileName=file.getOriginalFilename();

FiletargetFile=new File(path,fileName);

if(!targetFile.exists()){

targetFile.mkdirs();

}

file.transferTo(targetFile);

// request.setAttribute("filePath",targetFile.getAbsolutePath());

return targetFile.getAbsolutePath();

}

}

二、android客户端

 

 

1.导入上传需要的JAR包httpmime-4.1.1.jar

 

2.写一个工具类使用HttpClient

public class AndroidUploadFile {

/**

*

* @param filePath 文件地址

* @param urlServer 服务器的地址

* @return

* @throws IOException

* @throws ClientProtocolException

*/

publicstatic String uoloadFile(StringfilePath,String urlServer) throws ClientProtocolException, IOException{

//使用HttpClient

HttpClienthttpClient=new DefaultHttpClient();

//必须设置请求的协议

httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);

//设置http post请求

HttpPost httpPost=new HttpPost(urlServer);

//构建上传的文件的实体

MultipartEntity multipartEntity=new MultipartEntity();

//构建文件的File的对象

Filefile=new File(filePath);

//添加文件的

ContentBodycontentBody=new FileBody(file);

multipartEntity.addPart("file",contentBody);//<input type="file" name="file">

//把请求实体设置到HttpPost

httpPost.setEntity(multipartEntity);

//执行这个请求

HttpResponseresponse=httpClient.execute(httpPost);

//通过响应取到状态行

StatusLinestatusLine= response.getStatusLine();

//通过状态码去判断

if(statusLine.getStatusCode()==HttpStatus.SC_OK){

HttpEntity entity=response.getEntity();

Stringresult=EntityUtils.toString(entity);

Log.i("TAG","*******"+result);

}else{

Log.i("TAG","请求出了问题");

}

returnnull;

}

}

3.在UI线程调用工具类方法,注意,耗时操作必须另外开启一个工作线程执行

public  void doClick(View v){
String path=Environment.getExternalStorageDirectory().getAbsolutePath();
final File file=new File(path,"test.txt");
new Thread(){
public void run() {
try {
AndroidUploadFile.uoloadFile(file.getAbsolutePath(), "http://10.8.30.81:8080/SpringMvcUpload/mobile/uploadfile");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();