日期:2014-05-18  浏览次数:20950 次

如何通过http协议上传文件
服务器端使用的是tomcat,如何从客户端向服务器端上传文件?我不想做成任何网页的形式,只是通过客户端编程(如:使用HttpURLConnection)的方法来实现。

------解决方案--------------------
mySmartUpload.initialize(pageContext);
//mySmartUpload.service(request,response);
//Set Restriction
mySmartUpload.setAllowedFilesList( "doc,pdf,txt,jpg,xls,ppt,pps ");
//mySmartUpload.setAllowedFilesList( "jpg ");
mySmartUpload.setTotalMaxFileSize(5120000);
// Upload
mySmartUpload.upload();
}catch(Exception e){
System.out.println( " <font color=red> Upload File Fail! </font> <a href= \ "JavaScript:window.history.back();\ "> back </a> <br> ");
e.printStackTrace();
//return;
}
upload.File myFile = mySmartUpload.getFiles().getFile(0);
if (!myFile.isMissing()){
String aa=getServletContext().getRealPath( "/ ")+ "uploaddata\\ ";
out.println(aa);
String myFileName=myFile.getFileName();
int position = 0;
for(int i=(myFileName.length()-1);i> =0;i--){
if(myFileName.charAt(i)== '. '){
position = i;
break;
}
}
surfixname = myFileName.substring(position,myFileName.length());
uploadFileName = new xy.RandomGUID().toString()+surfixname;
String trace = aa+uploadFileName;
System.out.println( "aa= "+aa);
System.out.println( "uploadFileName= "+uploadFileName);
myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
System.out.println( "文件上传成功 ");