struts中使用FormFile文件上传

用贯了spring mvc的注入式文件上传,回到struts中都忘了怎么写,翻了翻老项目,记录下。
struts config中,定义formBean,action中用name指定formBean。

<struts-config>
    <form-beans>
        <form-bean name="fileManagerForm" type="com.dorole.FileManagerForm" />
    </form-beans>
    <action path="..." type="..." parameter="method" name="fileManagerForm">
        <forward name="..." path="..."></forward>
    </action>
</struts-config>

FileManagerForm如下

public class FileManagerForm extends ActionForm {
    private FormFile file;
    public void setFile(FormFile file) {
        this.file = file;
    }
    public FormFile getFile() {
        return file;
    }
}

FileManagerAction如下

public ActionForward upload(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FileManagerForm fmf = (FileManagerForm) form;
        FormFile formFile = fmf.getFile();
        if (formFile.getFileData().length != 0) {
            ...
        }
        return null;
}

jsp如下

<form action="..." method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="upload" />
</form>

Ubuntu Linux中连接Android真机调试

首先确保用数据线链接后能识别设备,输入:lsusb,可以看到类似输出

dorole@ubuntu:~$ lsusb
Bus 002 Device 006: ID 0bb4:0c87 High Tech Computer Corp.
Bus 002 Device 004: ID 0402:9665 ALi Corp.
Bus 002 Device 003: ID 04fc:05da Sunplus Technology Co., Ltd
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0951:1643 Kingston Technology
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

这样手机的vendor id和描述就出来了,第一条即是,htc的机器硬件id一般都是0bb4。

在/etc/udev/rules.d/目录下建立50-android.rules规则文件,内容如下:

SUBSYSTEM==”usb”, ATTR{idVendor}==”0bb4″, MODE=”0666″

修改后重启udev服务

dorole@ubuntu:/$ sudo /etc/init.d/udev restart

切换到android sdk的platform-tools目录下,重启adb服务,再输入adb devices就应该能找到设备了。

dorole@ubuntu:~/android-sdk-linux_x86/platform-tools$ sudo ./adb kill-server
dorole@ubuntu:~/android-sdk-linux_x86/platform-tools$ sudo ./adb start-server
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
dorole@ubuntu:~/android-sdk-linux_x86/platform-tools$ sudo ./adb devices
List of devices attached
SH0CDPL00575    device

这样就可以在eclipse中的DDMS来调试android真机了,有些步骤是需要root权限的。

Spring MVC – 高级 @RequestMapping 选项

  除了使用URL模板之外,@RequestMapping 注解也支持 Ant-style 风格的路径(例如:/myPath/*.do)。两种风格混合使用也是支持的(例如:/owners/*/pets/{petId})。

  如果路径没有明确指定映射,这时候就靠方法名来识别,依照 MethodNameResolver 类来处理(默认情况下是 InternalPathMethodNameResolver 类),这只适用于注解的路径不匹配请求的路径。换句话说,只用于减小其中一系列的匹配规则,不构成映射的首选。

  如果有一个没有指明映射路径的方法,那么同样所有的没有指明映射方法的请求都将调用它。如果有多个方法,也会依据方法名来选择。

  可以在请求路径中加入些参数来减小映射范围,例如加上"myParam=myValue"之类的参数(注:类似 struts 的method=methodName),这样请求就会映射到存在对应value参数的方法,例如:

@Controller 
@RequestMapping("/owners/{ownerId}") 
public class RelativePathUriTemplateController {
  @RequestMapping(value = "/pets/{petId}", params="myParam=myValue") 
  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {    
    // implementation omitted 
  } 
}

  "myParam" 这样的参数风格是支持的,当在请求中的参数中包含的时候(可以使用任何的值)。最后说下,"!myParam"参数风格指定了它不应该出现在请求路径中。

  同样的,请求头(header)也能用于减小映射路径范围。

@Controller 
@RequestMapping("/owners/{ownerId}") 
public class RelativePathUriTemplateController {
  @RequestMapping(value = "/pets", method = RequestMethod.POST, headers="content-type=text/*") 
  public void addPet(Pet pet, @PathVariable String ownerId) {    
    // implementation omitted 
  } 
}

  在上面的例子中,addPet() 方法只在 content-type 匹配 text/* 才会被调用,例如:text/xml。

  原文:link

Spring MVC – URI Templates

  在spring mvc中通过url路径来访问控制器中的方法,可以在用@RequestMapping注解的路径中使用URI模板。使用@PathVariable注解来指明方法中的参数应该对应URI模板中的变量,如下例子:

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
  Owner owner = ownerService.findOwner(ownerId);  
  model.addAttribute("owner", owner);  
  return "displayOwner"; 
}

  URI模板”/owners/{ownerId}”指定了变量名为ownerId。当方法被请求的时候ownerId的值会被赋值为请求的URI,比如一个请求为/owners/fred,那么方法中的ownerId参数会赋值为fred。必须保证参数名和URI模板变量名一致才能自动赋值,想自定义参数变量需要在@PathVariable注解中加入参数,如下:

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable("ownerId") String theOwner, Model model) {
  // implementation omitted
}

  当然,也可以使用多个@PathVariable来绑定多个URI模板变量,如下:

@RequestMapping(value="/owners/{ownerId}/pets/{petId}", method=RequestMethod.GET)
public String findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
  Owner owner = ownerService.findOwner(ownderId);  
  Pet pet = owner.getPet(petId);  
  model.addAttribute("pet", pet);  
  return "displayPet"; 
}

  下面的代码展示使用变量作为相对路径,当请求为/owners/42/pets/21,会调用findPet()方法。

@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
  @RequestMapping("/pets/{petId}")
  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {    
    // implementation omitted
  }
}

  @PathVariable和方法中的参数可以是任何简单数据类型,例如:int,long,Date,等等。spring会自动转换,如果不匹配则抛出TypeMismatchException。

  原文:link

java使用google网址缩短

package com.dorole.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public class Googl
{
	private static String googUrl = "https://www.googleapis.com/urlshortener/v1/url";

	public static String shorten(String longUrl)
	{
		String shortUrl = "";
		try
		{
			URLConnection conn = new URL(googUrl).openConnection();
			conn.setDoOutput(true);
			conn.setRequestProperty("Content-Type", "application/json");
			OutputStreamWriter osw = new OutputStreamWriter(
					conn.getOutputStream());
			osw.write("{\"longUrl\":\"" + longUrl + "\"}");
			osw.flush();

			BufferedReader br = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			String line;
			while ((line = br.readLine()) != null)
			{
				if (line.indexOf("id") > -1)
				{
					shortUrl = line.substring(8, line.length() - 2);
					break;
				}
			}
			osw.close();
			br.close();
		} catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return shortUrl;
	}

	public static void main(String[] args)
	{
		String url = Googl.shorten("http://dorole.com");
		System.out.println(url);
	}
}

需要的地方调用Googl.shorten(), 参考:link, 引自:link