客户端部分功能代码:
package com.idreader;
import java.io.File ;
import org.apache.commons.httpclient.HttpClient ;
import org.apache.commons.httpclient.HttpStatus ;
import org.apache.commons.httpclient.methods.PostMethod ;
import org.apache.commons.httpclient.methods.multipart.FilePart ;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity ;
import org.apache.commons.httpclient.methods.multipart.Part ;
public class HttpPicClient
{
/*
* private Context mContext ;
*
* public Hclient ( Context c ) { this.mContext = c ; }
*/
public void UpLoadFile ( String filepath,String serviceUrl)
{
String targetURL = null ;// TODO 指定URL
File targetFile = null ;// TODO 指定上传文件
targetFile = new File ( filepath) ;
targetURL = serviceUrl; // "http://10.10.10.158:8090/IDServer/TestServlet";servleturl
PostMethod filePost = new PostMethod ( targetURL ) ;
try
{
// 通过以下方法可以模拟页面参数提交
// filePost.setParameter("name", "中文");
// filePost.setParameter("pass", "1234");
Part [ ] parts =
{ new FilePart ( targetFile.getName ( ) , targetFile ) } ;
filePost.setRequestEntity ( new MultipartRequestEntity (
parts , filePost.getParams ( ) ) ) ;
HttpClient client = new HttpClient ( ) ;
client.getHttpConnectionManager ( ).getParams ( )
.setConnectionTimeout ( 5000 ) ;
int status = client.executeMethod ( filePost ) ;
if ( status == HttpStatus.SC_OK )
{
System.out.println ( "上传成功" ) ;
// 上传成功
}
else
{
System.out.println ( "上传失败" ) ;
// 上传失败
}
}
catch ( Exception ex )
{
ex.printStackTrace ( ) ;
}
finally
{
filePost.releaseConnection ( ) ;
}
}
}
服务端部分功能代码:
package com.oliver.web.controller;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import com.oliver.entity.Account;
import com.oliver.service.AccountService;
import com.oliver.service.AccountServiceImpl;
public class UploadFileController extends AbstractController {
private String successView;
private String failView;
public String getSuccessView() {
return successView;
}
public void setSuccessView(String successView) {
this.successView = successView;
}
public String getFailView() {
return failView;
}
public void setFailView(String failView) {
this.failView = failView;
}
//定义上传路径
private String uploadPath = "D:\\TempSpringMVCUploadFiles" ; // 上传文件的目录
public void init( ) throws ServletException
{
File uploadFile = new File ( uploadPath ) ;
if ( ! uploadFile.exists ( ) )
{
uploadFile.mkdirs ( ) ;
}
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
init();
//创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//判断 request 是否有文件上传,即多部分请求
if(multipartResolver.isMultipart(request)){
//转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
//取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while(iter.hasNext()){
//记录上传过程起始时的时间,用来计算上传时间
int pre = (int) System.currentTimeMillis();
//取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
if(file != null){
//取得当前上传文件的文件名称
String myFileName = file.getOriginalFilename();
//如果名称不为“”,说明该文件存在,否则说明该文件不存在
if(myFileName.trim() !=""){
System.out.println(myFileName);
//重命名上传后的文件名
String fileName = "demoUpload" + file.getOriginalFilename();
File localFile = new File ( uploadPath ,
fileName ) ;
file.transferTo(localFile);
}
}
//记录上传该文件后的时间
int finaltime = (int) System.currentTimeMillis();
System.out.println(finaltime - pre);
}
return new ModelAndView(getSuccessView());
}else{
return new ModelAndView(getFailView());
}
}
}