Monthly Archives: 4月 2015

ASIHTTPRequestでhttpsにアクセスする方法

方法1
ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:[NSURLURLWithString:bodyString]];

[request setDelegate:self];

request.timeOutSeconds = 20;

[request setRequestMethod:@”POST”];

[request addRequestHeader:@”Content-Type”value:@”application/xml;charset=UTF-8;”];

[request setValidatesSecureCertificate:NO];//httpsにアクセス

[request startAsynchronous];

方法2

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:bodyString]];
[request setDelegate:self];
request.timeOutSeconds = 20;
[request setValidatesSecureCertificate:NO];
[request setRequestMethod:@”POST”]; [request addRequestHeader:@”Content-Type” value:@”application/xml;charset=UTF-8″]; request.defaultResponseEncoding = NSUTF8StringEncoding;

「Android開発」ダブルクリック戻る、アプリケーションを終了する方法

1.ダブりクリック処理クラスの定義
DoubleClickExitHelper_startnews24.java

public class DoubleClickExitHelper_startnews24
{
private final Activity mActivity;
private boolean isOnKeyBacking;
private Handler mHandler;
private Toast mBackToast;
public DoubleClickExitHelper_startnews24(Activity activity)
{
mActivity = activity;//activityを保存
mHandler = new Handler(Looper.getMainLooper());//handlerを生成し、メッセージキューを管理
}

/**
* Activity onKeyDown関数
* */
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode != KeyEvent.KEYCODE_BACK)
{
return false;
}
if (isOnKeyBacking)
{
mHandler.removeCallbacks(onBackTimeRunnable);//メッセージキューを削除
if (mBackToast != null)
{
mBackToast.cancel();
}
//終了
mActivity.finish();
return true;
}
else
{
isOnKeyBacking = true;
if (mBackToast == null)
{
mBackToast = Toast.makeText(mActivity, R.string.back_exit_tips, 2500);
}
mBackToast.show();
mHandler.postDelayed(onBackTimeRunnable, 2500);//2500ms後に実行
return true;
}
}

// isOnKeyBackingをfalseに設定
private Runnable onBackTimeRunnable = new Runnable()
{
@Override
public void run()
{
isOnKeyBacking = false;
if (mBackToast != null)
{
mBackToast.cancel();
}
}
};
}

2.メインインタフェース呼び出し
Main.java
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ダブルクリック戻るキーを終了
mDoubleClickExitHelper_startnews24 = new DoubleClickExitHelper_startnews24(this);
}

/**
* プログラムを終了するかどうか
*/
public boolean onKeyDown(int keyCode, KeyEvent event)
{
boolean flag = true;
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// アプリケーションを終了するかどうか
return mDoubleClickExitHelper_startnews24.onKeyDown(keyCode, event);
}
else if (keyCode == KeyEvent.KEYCODE_MENU)
{
//
}
else if (keyCode == KeyEvent.KEYCODE_SEARCH)
{
//
}
else
{
//
}
return flag;
}

「Java開発」SpringMVCでファイルダウンロードを実現する方法

Javaコード:
package com.sniper.springmvc.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sniper.springmvc.utils.ValidateUtil;

@Controller
public class FileDownloadAction extends RootAction {

// mime//application/vnd_startnews24.ms-excelダウンロード
private String contentType = “application/vnd_startnews24.ms-excel”;
private String fileName;
private String filePath;

public String getFileName() throws UnsupportedEncodingException {
fileName = filePath.substring(filePath.lastIndexOf(“/”) + 1);
String Agent = request.getHeader(“User-Agent”);
if (null != Agent) {
Agent = Agent.toLowerCase();
if (Agent.indexOf(“firefox”) != -1) {
fileName = new String(fileName.getBytes(), “iso8859-1”);
} else if (Agent.indexOf(“msie”) != -1) {
fileName = URLEncoder.encode(fileName, “UTF-8”);
} else {
fileName = URLEncoder.encode(fileName, “UTF-8”);
}
}
return fileName;
}
/**
* redirect:downloadでファイルパスやファイルタイプを転送
*
* @param path
* @param type
* @return
* @throws IOException
*/
@ResponseBody
@RequestMapping(value = “download”)
public ResponseEntity<byte[]> download(
@RequestParam(value = “path”) String path,
@RequestParam(value = “type”, required = false) String type)
throws IOException {
// メンバー変数の値を設定
this.filePath = path;
if (ValidateUtil.isValid(contentType)) {
this.contentType = type;
}
HttpHeaders headers = new HttpHeaders();
byte[] body = null;
HttpStatus httpState = HttpStatus.NOT_FOUND;
File file = new File(path);
if (file.exists() && file.isFile()) {
InputStream is = new FileInputStream(file);
body = new byte[is.available()];
is.read(body);
is.close();
headers.add(“Content-Type”, this.contentType);
headers.add(“Content-Length”, “” + body.length);
headers.add(“Content-Disposition”, “attachment;filename=”
+ getFileName());
httpState = HttpStatus.OK;
}
ResponseEntity<byte[]> entity = new ResponseEntity<>(body, headers,
httpState);
return entity;
}
}

「Java開発」SpringMVCでファイルをダウンロードするソースコード

Javaコード
@RequestMapping(params = “downfile”)
public void downfile(HttpServletRequest request,HttpServletResponse response) throws Exception{
Long fileID = ServletRequestUtils.getLongParameter(request, “id”, 0);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
String result = “”;
if(null != fileID){

UserFiles userFiles = this.userFileDao.findObject(fileID);
if(userFiles !=null){
File downFiles = new File(userFiles.getFileCurrPath()+userFiles.getFileName()+”.”+userFiles.getFileType());
if(!downFiles.exists()){

return;

}
try {
fis = new FileInputStream(downFiles);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
ServletUtils.setFileDownloadHeader(request,response, userFiles.getFileName()+”.”+userFiles.getFileType());
int byteRead = 0;
byte[] buffer = new byte[8192];
while((byteRead=bis.read(buffer,0,8192))!=-1){
bos.write(buffer,0,byteRead);
}

bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (Exception e) {
log.info(“ダウンロード失敗した……”);
}
}
}

}

public static void setFileDownloadHeader(HttpServletRequest request,HttpServletResponse response, String fileName) {
try {
String encodedfileName = null;
String agent = request.getHeader(“USER-AGENT”);
if(null != agent && -1 != agent.indexOf(“MSIE”)){//IE
encodedfileName = java.net.URLEncoder.encode(fileName,”UTF-8″);
}else if(null != agent && -1 != agent.indexOf(“Mozilla”)){
encodedfileName = new String (fileName.getBytes(“UTF-8″),”iso-8859-1″);
}else{
encodedfileName = java.net.URLEncoder.encode(fileName,”UTF-8”);
}
response.setHeader(“Content-Disposition”, “attachment; filename=\”” + encodedfileName + “\””);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

「Ruby」Ubuntu14.04でホスト名を変更するサンプルコード

Rubyコード
#!/usr/bin/ruby
#coding=utf-8
require ‘open-uri’

url= “http ://raw.github usercontent.com/ vokins/simpleu/master/hosts”

begin
open(“/etc/hosts”,”w”) do |x|
x.write open(url).read
end
rescue Errno::EACCES
puts “root必要? ”
exec “sudo ruby ” + $0 + ARGV.join(‘ ‘)
end
puts “ホストの情報を変更した!”

負荷分散ソフトウェアBalanceNG 3.600 ダウンロード

ダウンロード

更新内容

マニュアルの更新
“server N tcprefuse”コマンドの追加
IPV4とIPv6のTCP接続拒否の同意

フォントエディタBirdfont 2.5ダウンロード

ダウンロード
開発元からダウンロード
更新内容
エクスポートの設定
Curveポジショニング

Linux Kernel 3.19.5/3.14.39/3.10.75ダウンロード

ダウンロード

3.19.5[tar.xz][pgp][patch][inc. patch][view diff][browse][changelog]

3.14.39[tar.xz][pgp][patch][inc. patch][view diff][browse][changelog]

3.10.75[tar.xz][pgp][patch][inc. patch][view diff][browse][changelog]