Xutils上传图片(2)

1、
(1)、files

/*
     * 创建一个以当前系统时间为名称的文件,防止重复----------(0)
     */
    private File tempFile = new File(Environment.getExternalStorageDirectory(),getPhotoFileName());

    // 使用系统当前日期加以调整作为照片的名称
    private String getPhotoFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("'PNG'_yyyyMMdd_HHmmss");
        return sdf.format(date) + ".png";
    }

(2)、http发送请求:
//设置了相应时间为:10000
httpUtils=new HttpUtils(10000);

// 上传文件到服务器
    protected void upload() {
        RequestParams params=new RequestParams();
        params.addBodyParameter(tempFile.getPath().replace("/", ""), tempFile);
        httpUtils.send(HttpMethod.POST,URL, params,new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException e, String msg) {
                Toast.makeText(MainActivity.this, "上传失败,检查一下服务器地址是否正确", Toast.LENGTH_SHORT).show();
                Log.i("MainActivity", e.getExceptionCode() + "====="
                        + msg);
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Toast.makeText(MainActivity.this, "上传成功,马上去服务器看看吧!", Toast.LENGTH_SHORT).show();
                Log.i("MainActivity", "====upload_error====="
                        + responseInfo.result);
            }
        });
    }

(3)、服务器接受文件流(smartupload接受)

public class UploadServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        SmartUpload smartUpload = new SmartUpload();
        String msg=request.getParameter("msg");
        //out.print(msg);  
        try {  
            smartUpload.initialize(this.getServletConfig(), request, response); 
            smartUpload.upload();  
            com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0);  
            if (!smartFile.isMissing()) {  
                String saveFileName = "images/" + smartFile.getFileName();  
                smartFile.saveAs(saveFileName, smartUpload.SAVE_VIRTUAL);  
                out.print("ok:" + saveFileName+ ", msg:" + smartUpload.getRequest().getParameter("msg"));
            } else {  
                out.print("missing...");  
            }  
        } catch (Exception e) {  
            out.print(e+","+msg);  
        } 
        out.flush();
        out.close();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值