//如果表达enctype="multipart/form-data",则servlet中无法获得参数值,所以下面代码打印为null
System.out.println(request.getParameter("username"));
InputStream in = request.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len=in.read(buffer))>0){
System.out.println(new String(buffer));
}
ServletFileUpload中的setHeaderEncoding()
public void setHeaderEncoding(String encoding)
Specifies the character encoding to be used when reading the headers of individual part. When not specified, or null, the request encoding is used. If that is also not specified, or null, the platform default encoding is used.
Parameters:
encoding - The encoding used to read part headers.
________________________________________
upload.setHeaderEncoding("utf-8");
2、 上传的普通输入项的乱码
手工转码
用户名的乱码问题
paramValue= new String(paramValue.getBytes("iso8859-1"),"utf-8");
利用FileItem类的getString(String encoding)
String getString(String encoding)
throws UnsupportedEncodingException
Returns the contents of the file item as a String, using the specified encoding. This method uses get() to retrieve the contents of the item.
解决没有指定文件名的问题
判断获取的文件名是否为空
临时文件的删除问题
FileItem
void delete()
Deletes the underlying storage for a file item, including deleting any associated temporary disk file. Although this storage will be deleted automatically when the FileItem instance is garbage collected, this method can be used to ensure that this is done at an earlier time, thus preserving system resources.
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(this.getServletContext().getRealPath("/temp")));
……
FileItem item;
……
item.delete();
//上面代码必须放在流关闭语句的最后面,因为正在使用的文件是不能删除的