一、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();