Servelet上传时,只出现一个汉字的错误

,Servlet上传时,编码处理的问题,一个老问题,记下来警示自己和他人,
package com.hysoft.danbao;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.hysoft.utils.InsertFile;




public class UploadServlet extends HttpServlet {
       //初始化条件             
InputStream is=null;
String clmc=null;
String ysr=null;
String ysyj=null;
String cllj=null;
String uploadname=null;
String uploadname1=null;
int kehu_id=1;


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();


boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart == true) {
    //创建该对象
       FileItemFactory factory = new DiskFileItemFactory();
       ServletFileUpload upload = new ServletFileUpload(factory);
       List<FileItem> items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
//解析request 请求,并返回FileItemIterator集合
       Iterator<FileItem> itr = items.iterator();
       while (itr.hasNext()) {
    //从集合中获得一个文件流
           FileItem item = (FileItem) itr.next();
     
           if (item.isFormField()) {
       String fieldName = item.getFieldName();
       if ("clmc".equals(fieldName)) {
    clmc=new String(item.getString().getBytes("iso-8859-1"), "GBK");
       }else if("ysr".equals(fieldName)){
       ysr=new String(item.getString().getBytes("iso-8859-1"), "GBK");
       }else if("ysyj".equals(fieldName)){
       ysyj=new String(item.getString().getBytes("iso-8859-1"), "GBK");
       }else if("kehu_id".equals(fieldName)){
       kehu_id=Integer.parseInt(item.getString());
       }
      
           }
           else {
       
        uploadname=new String(item.getName().getBytes("iso-8859-1"), "GBK");
       
        uploadname1=uploadname.substring(uploadname.lastIndexOf("."));
        is=item.getInputStream();
       
}
       }
    } else {
     System.out.println(" form error");
    }
System.out.println(clmc);
System.out.println(ysr);
System.out.println(ysyj);



String realpath=request.getRealPath("/upload");

File uploaddir =new File(realpath);
if(!uploaddir.exists()){
uploaddir.mkdir();
}
String savename=UUID.randomUUID().toString()+uploadname1;
String savepath=realpath+ File.separator+savename;
cllj="upload/"+savename;
OutputStream out1 = new FileOutputStream(savepath);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) > 0) {
out1.write(buffer, 0, len);
}
out1.close();
is.close();
System.out.println(kehu_id);
System.out.println(cllj);
InsertFile.add(clmc,ysr,ysyj,cllj,kehu_id);
out.print("文件上传成功!<br/><br/>");
out.print("<input type='button' onclick='window.close();window.opener.location.reload();' value='关闭窗口'>");


}

}

你可能感兴趣的:(上传)