import org. apache. commons. fileupload. FileItem;
import org. apache. commons. fileupload. FileUploadException;
import org. apache. commons. fileupload. disk. DiskFileItemFactory;
import org. apache. commons. fileupload. servlet. ServletFileUpload;
import javax. servlet. ServletException;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
import java. io. File;
import java. io. IOException;
import java. util. List;
public class SyncAntiPayeeC {
protected void download ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory ( ) ;
ServletFileUpload upload = new ServletFileUpload ( factory) ;
boolean isMultipart = ServletFileUpload. isMultipartContent ( request) ;
if ( ! isMultipart) {
System. out. println ( "ok" ) ;
} else {
try {
List< FileItem> fileItems = upload. parseRequest ( request) ;
for ( FileItem fileItem : fileItems) {
if ( fileItem. isFormField ( ) ) {
System. out. println ( fileItem. getFieldName ( ) + "\t" + fileItem. getString ( "UTF-8" ) ) ;
} else {
String fileName = fileItem. getName ( ) ;
System. out. println ( fileName) ;
String ext = fileName. substring ( fileName. lastIndexOf ( "." ) ) ;
String name = java. util. UUID. randomUUID ( ) + ext;
File file = new File ( "D:/workspace" + File. separator + name) ;
fileItem. write ( file) ;
}
}
} catch ( FileUploadException e) {
e. printStackTrace ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}
}
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String syncAnti = request. getParameter ( "param" ) ;
if ( "file" . equals ( syncAnti) ) {
SyncAntiPayeeC syncAntiPayee = new SyncAntiPayeeC ( ) ;
syncAntiPayee. download ( request, response) ;
return ;
}
}
< html>
< head>
< title> upload</ title>
< meta http-equiv = " pragma" content = " no-cache" >
< meta http-equiv = " cache-control" content = " no-cache" >
< meta http-equiv = " expires" content = " 0" >
< meta http-equiv = " keywords" content = " keyword1,keyword2,keyword3" >
< meta http-equiv = " description" content = " This is my page" >
</ head>
< body>
< form action = " /claim/GcRiskClaimServlet.do?param=file" enctype = " multipart/form-data" method = " post" >
< input type = " file" name = " file" />
< input type = " submit" value = " 提交" />
</ form>
</ body>
</ html>