您好,登錄后才能下訂單哦!
一丶首先服務端的代碼:
public class UploadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8") ;
response.setContentType("text/html;charset=UTF-8") ;
PrintWriter out = response.getWriter() ;
try {
//1.指定上傳的目錄
String storeDirectory = getServletContext().getRealPath("/WEB-INF" + File.separator + "files") ;
//2.判斷目錄不存在則創建出來
File file = new File(storeDirectory) ;
if(!file.exists())
file.mkdirs() ;
//3. 創建上傳的servletFileuload類
DiskFileItemFactory dif = new DiskFileItemFactory() ; //使用默認的臨時目錄
ServletFileUpload sfu = new ServletFileUpload(dif) ;
//多判斷一步
boolean isMultipart = sfu.isMultipartContent(request) ;
if(!isMultipart){
out.write("上傳的enctype必須是multipart/form-data類型") ;
return ;
}
//String newDirectory = makeNewDirectory(storeDirectory);
//設置單個文件的上傳大小(限制上傳文件的大小不的超過2M)
// sfu.setFileSizeMax(2*1024*1024) ;
//設置上傳總文件的大小(限制上傳文件的總的大小不的超過10M))
// sfu.setSizeMax(10*1024*1024) ;
//4. 解析請求
List<FileItem> items = sfu.parseRequest(request) ;
//5.判斷是普通字段還是上傳字段,分別處理
for (FileItem item : items) {
//依次判斷
if(item.isFormField()){
//說明是普通字段
String name = item.getFieldName() ; //拿到文本框的名字
String value = item.getString("UTF-8") ; //拿到文本框中的內容
System.out.println(name + "=" + value);
}else{
//說明是上傳字段
//限定上傳的文件只能是圖片
// if(item.getContentType().contains("p_w_picpath")){
//說明上傳的是圖片
//拿到上傳的文件的名字
String filename = item.getName() ; //拿到文件名 :不包括路徑
//如果文件名不是""則上傳
System.out.println("上傳文件名;" + filename);
//if("".equals(filename)){
filename = UUID.randomUUID() + "_" + filename ;
//采用文件名的hashcode計算目錄名
String newDirectory = makeNewDirectory(storeDirectory,filename);
//System.out.println("filename:" + filename);
//System.out.println("aaaa:" + (storeDirectory + File.separator + newDirectory + File.separator + filename));
//構建服務器上的文件
File f = new File(storeDirectory + File.separator + newDirectory + File.separator + filename) ;
item.write(f) ;
//}
// }else{
// out.write("只能上傳圖片") ;
// return ;
// }
}
}
out.write("上傳成功") ;
}catch (FileUploadBase.FileSizeLimitExceededException e1) {
out.write("上傳文件不得大于2M") ;
}catch (FileUploadBase.SizeLimitExceededException e1) {
out.write("上傳總文件不得大于10M") ;
}catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//用文件名的hashcode計算文件目錄
private String makeNewDirectory(String storeDirectory,String filename) {
//拿到哈希碼
int hashcode = filename.hashCode() ;
int dir1 = hashcode&0xf;
int dir2 = (hashcode&0xf0)>>4;
String newDirectory = File.separator + "" + dir1 + File.separator + dir2 ;
System.out.println(newDirectory);
//判斷目錄是否存在
File fdate = new File(storeDirectory,newDirectory) ;
System.out.println("bb:" +fdate.getAbsolutePath());
if(!fdate.exists())
fdate.mkdirs() ;
return "" + dir1 + File.separator + dir2 ;
}
//創建日期目錄
private String makeNewDirectory(String storeDirectory) {
Date now = new Date() ;
String newDirectory = new SimpleDateFormat("yyyyMMdd").format(now) ;
File fdate = new File(storeDirectory,newDirectory) ;
if(!fdate.exists())
fdate.mkdirs() ;
return newDirectory;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Content-Type", "text/html;charset=UTF-8");
doGet(request, response);
}
}
二丶android上傳代碼:使用API中的AsyncHttpClient連接服務器
public void click(View view){
//拿到上傳文件的路徑
String path = etpath.getText().toString().trim() ;
//判斷路徑
if(TextUtils.isEmpty(path)){
Toast.makeText(this, "路徑不能為空", 0).show() ;
return ;
}
try {
//連接服務器,上傳文件
String serverPath = "http://192.168.1.184:8080/upload/servlet/UploadServlet" ;
//創建連接對象
AsyncHttpClient http = new AsyncHttpClient() ;
//設置提交的數據
RequestParams params = new RequestParams() ;
params.put("file", new File(path)) ;
//發送post請求
http.post(serverPath, params, new AsyncHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers,
String content) {
super.onSuccess(statusCode, headers, content);
Toast.makeText(MainActivity.this, "上傳文件成功", 0).show() ;
}
}) ;
} catch (Exception e) {
e.printStackTrace();
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。