- 保存数据到文件
- 上传文件到七牛云
//保存为json文件 String desktopDir = System.getProperty("user.home") + "\Desktop\"; //本地路径 JSonObject jsonObject = new JSONObject(); BufferedWriter writer = null; File file = new File(desktopDir + "all_freeip.json"); //如果文件不存在,则新建一个 if(!file.exists()){ try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } //写入 try { writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8")); writer.write(jsonObject.toJSONString(ips)); } catch (IOException e) { e.printStackTrace(); }finally { try { if(writer != null){ writer.close(); } } catch (IOException e) { e.printStackTrace(); } } logger.info("json文件写入成功!");上传文件到七牛云
import java.io.IOException; import com.qiniu.common.QiniuException; import com.qiniu.common.Zone; import com.qiniu.http.Response; import com.qiniu.storage.Configuration; import com.qiniu.storage.UploadManager; import com.qiniu.util.Auth; import com.qiniu.util.StringMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class QiniuUploadQuartz { private Logger logger = LoggerFactory.getLogger(this.getClass()); public static final String ACCESS_KEY = "xxxxxx"; //你的access_key public static final String SECRET_KEY = "xxxxxx"; //你的secret_key public static final String BUCKET_NAME = "xxxxxx"; //要上传的空间 //本地路径 String desktopDir = System.getProperty("user.home") + "\Desktop\"; //上传到七牛后保存的文件名 String key = "all_freeip.json"; //上传文件的路径 String FilePath = desktopDir + "all_freeip.json"; //本地要上传文件路径 //密钥配置 Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); // 构造一个带指定Zone对象的配置类 Configuration cfg = new Configuration(Zone.zone2()); //创建上传对象 UploadManager uploadManager = new UploadManager(cfg); public String getUpToken(){ // return auth.uploadToken(bucketname); //: ,表示只允许用户上传指定key的文件。在这种格式下文件默认允许“修改”,已存在同名资源则会被本次覆盖。 //如果希望只能上传指定key的文件,并且不允许修改,那么可以将下面的 insertonly 属性值设为 1。 //第三个参数是token的过期时间 return auth.uploadToken(BUCKET_NAME, key, 3600, new StringMap().put("insertOnly", 0 )); } public void upload() throws IOException{ try { //调用put方法上传 Response res = uploadManager.put(FilePath, key, getUpToken()); //打印返回的信息 System.out.println(res.bodyString()); } catch (QiniuException e) { Response r = e.response; // 请求失败时打印的异常的信息 logger.info(r.toString()); try { //响应的文本信息 logger.info(r.bodyString()); } catch (QiniuException e1) { //ignore } } } public static void main(String args[]) throws IOException { new QiniuUploadQuartz().upload(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)