当前位置:编程学习 > JAVA >>

servlet文件接受怪异



这个是servlet文件上传的 文件,我表单提交


form:

<html>
  <head>
    <title>MyHtml.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
   <form name="for" action="http://localhost:8080/nt/StorageController/UploadFile.do" method="post"  enctype="multipart/form-data" >
   <input name="xxx" type="file"/>

   <input type="submit" value="tijiao">
   </form>
  
  </body>
</html>







servlet:


protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//audio,video,photo,text,other
//StorageServlet asd2=new StorageService();
String pathType = request.getParameter("pathType");//保存类型
String TxtName = request.getParameter("TxtName");//文件名字 例如:1.txt
TxtName="1.txt";
if(pathType==null||TxtName==null){
return;
}


String path = request.getServletContext().getRealPath("/");
String zuhepath = path + "/"+pathType+"/";
File file = new File(zuhepath);
if (!file.exists()) {// 没有文件夹创建文件夹
file.mkdirs();
}
String info = setReceiveFile(request, response, pathType, zuhepath,TxtName);
System.out.println(info);
}




/**
 * 接受文件
 * 
 * @param request
 * @param response
 * @param pathType
 *            文件类型
 * @param path
 *            存放地址
 * @throws UnsupportedEncodingException 
 */
public String setReceiveFile(ServletRequest request,ServletResponse response, 
String pathType, String path,String TxtName) throws UnsupportedEncodingException {
String uuid = UUID.randomUUID().toString();// 创建一个唯一id 当做文件名字
ServletInputStream inputStream = null;
FileOutputStream fos = null;
request.setCharacterEncoding("UTF-8");  
//Map<String, String> map= GetuploadFileInfo(request);
String fileName = uuid + "." +TxtName.substring(TxtName.lastIndexOf("."),TxtName.length() );// 文件名字
Integer temsize=0;
try {
inputStream = request.getInputStream();// 上传文件流
fos = new FileOutputStream(path + fileName);// 文件输入流
byte[] buffer = new byte[1024];
int len = 0;

while ((len = inputStream.read(buffer)) != -1) {
//System.out.println(len);
System.out.println();
fos.write(buffer, 0, len);
temsize+=buffer.length;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件接收错误:" + e.getMessage());
return jsonConver(false, e.getMessage());
} finally {// 关闭
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Storage storage=new Storage();
storage.setStorageId(null);
storage.setStorageSize(temsize.toString());//大小
storage.setStorageType(pathType);//文件类型
storage.setStoragePath(path + fileName);//文件路径
storage.setStorageCode(uuid);//文件唯一id

//storageService.insertSelective(storage);
return jsonConver(true, uuid);
}





/**
 * 转换json
 * 
 * @param b
 *            true 成功 false 失败
 * @param msg
 *            uuid 或者错误信息
 * @return
 */
public String jsonConver(boolean b, String msg) {
Map<String, String> map = new HashMap<String, String>();
if (b) {
map.put("success", "success");
map.put("uuid", msg);
} else {
map.put("success", "error");
map.put("msg", msg);
}
JSONObject jsonObject = (JSONObject) JSON.toJSON(map);
return jsonObject.toJSONString();
}














有什么办法去掉流里面的form表单信息,只要文件流 --------------------编程问答-------------------- 快来人啊,我顶
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,