1.FileUpload
下载地址:
Maven地址
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
介绍:
文件上传用
核心类 ServletFileUpload
使用方法:
如果要上传 form表单上 必须要给enctype属性
<form method="POST" enctype="multipart/form-data" action="/">
File to upload: <input type="file" name="upfile"><br/>
<br/> <input type="submit" value="Press"> to upload the file! </form>
如何获取到ServletFileUpload对象
DiskFileItemFactory factory = new DiskFileItemFactory();
// Configure a repository (to ensure a secure temp location is used)
ServletContext servletContext = this.getServletConfig().getServletContext();
File repository = (File) servletContext.getAttribute( "javax.servlet.context.tempdir");
factory.setRepository(repository);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
//在servlet中获取你要存放的路径
String path= request.getSession().getServletContext().getRealPath( "/upload");
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
} else {
String fieldName = item.getFieldName(); //表单的name
String fileName = item.getName(); //文件的名称
String contentType = item.getContentType(); //文件的类型
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize(); //文件的大小
InputStream uploadedStream = item.getInputStream(); //获取文件流
//生成随机的名称 防止文件因为重名而覆盖
String uuidname=UUID.randomUUID().toString().replace( "-", "");
FileUtils.copyInputStreamToFile(uploadedStream, new File(path,uuidname));
}
}
private InputStream inputStream;
private String filename;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
@Action(value = "getFile",results = {@Result(name="download",type="stream",params = {"inputName","inputStream","contentDisposition","attachment;filename=${filename}"})})
public String getFile() throws FileNotFoundException, UnsupportedEncodingException {
String realPath = ServletActionContext.getServletContext().getRealPath("/upload");
inputStream=new FileInputStream(new File(realPath,"mv.jpg"));
//中文乱码的的处理方式 中文不能再浏览器中传输
//字典 中文 英文字典 万能字典 繁体的字典
//中文 GBK 英文 ISO-8859-1 UTF-8 兼容所有的语言 台湾BIG5
//将文字转换为 字节 0.1.1.1 汉字->拼音
//1.浏览器不认识 中文 不认识中文 中文转换为字节的数组
filename=new String("美女很美".getBytes(),"ISO-8859-1");
return "download";
}
@Action(value = "getFile2")
public void getFile2() throws IOException {
String filename=new String("美女很美.jpg".getBytes(),"ISO-8859-1");
HttpServletResponse response = ServletActionContext.getResponse();
//response.setHeader("Content-Disposition","attachment;filename="+filename);
String realPath = ServletActionContext.getServletContext().getRealPath("/upload");
InputStream inputStream=new FileInputStream(new File(realPath,"mv.jpg"));
OutputStream outputStream = response.getOutputStream();
byte[] b=new byte[1024];
int len=0;
while((len=inputStream.read(b))!=-1)
{
outputStream.write(b,0,len);
outputStream.flush();
}
inputStream.close();
outputStream.close();
}
public static void main(String[] args) throws UnsupportedEncodingException {
String filename=new String("美女很美".getBytes(),"ISO-8859-1");
System.out.println(filename);
filename =new String(filename.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(filename);
}
2.Pinyin4j
下载地址:
http://pinyin4j.sourceforge.net/Maven地址
<!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
介绍:
目的是为了将我们的中文转换为拼音
使用方法:
//如果你想要转换的整个字 如果是多音字 返回多个
String[] pinyinArray =PinyinHelper.toHanyuPinyinStringArray('单');
封装一下
public class pinyinUtils{
public static String getPinyin(String word)
{
String pinyinString="";
//1.将汉字转换为字符数组
char[] words=word.toCharArray();
//2.一个一个的使用pinyinHelper转换字符
for(char w:words)
{
String[]pinyinArray=PinyinHelper.toHanyuPinyinStringArray(w);
//3. 忽略掉多音字 只拿第一个 非汉字的情况 就原样输出
if(pinyinArray!=null&&pinyinArray.length>0)
{
pinyinString+=pinyinArray[0];
}
else
{
pinyinString+=w;
}
}
return pinyinString;
}
}
http://commons.apache.org/proper/commons-beanutils/
Maven地址
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
介绍:
将map转换为bean 获取将bean转换为map
使用方法:
void BeanUtils.populate(Object bean, Map<String,? extends Object> properties) 将map的数据放到bean中 前提要先new一个bean
Map<String,String> BeanUtils.describe(Object bean) 将bean转换为map
4.IO
下载地址:
Maven地址
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
介绍:
用于文件的操作
使用方法:
copyInputStreamToFile(InputStream source,File destination); //将输入流变成写入到一个文件中
copyDirectoryToDirectory(File srcDir, File destDir); //拷贝一个文件夹到另外一个文件夹中
copyFileToDirectory(File srcFile,File destDir) ;//拷贝一个文件到文件夹
5.Gson
下载地址:
Maven地址
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.4</version>
</dependency>
介绍:
可以将对象转换为json字符串
可以将json转换为对象
一般用于ajax的操作
使用方法:
Gson gson=new Gson();
String jsonString=gson.toJson(obj);
gson.fromJson(jsonString,Class clz);