1.SpringMvc上传文件后台
一、通过文件流的方式上传
二、通过表单形式提交接收
@ResponseBody
@RequestMapping(value="uploadnews.do",method=RequestMethod.POST)
public String addNews(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式
SimpleDateFormat date=new SimpleDateFormat("yyyyMMddHHmmss");
File dir=new File(Constants.UPLOAD.UPLOAD_URL_DIRURL);
// 文件夹不存在即创建
if (!dir.exists() && !dir.isDirectory()) {
dir.mkdirs();
}
//数据存储
String firsttitle="";
String twotitle="";
String newuser="";
String newsinfo="";
String newtime=sdf.format(new Date());
String newstype="";
int newscount=0;
StringBuffer newsimg=new StringBuffer();
/**
* 创建工厂类
*/
request.setCharacterEncoding("utf-8");
ServletRequestContext requestContext = new ServletRequestContext(request);
if(FileUpload.isMultipartContent(requestContext)){
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(dir);//设置保存目录
ServletFileUpload upload = new ServletFileUpload(factory);
//upload.setHeaderEncoding("utf-8");
upload.setSizeMax(2000000);
List items = new ArrayList();
try {
items = upload.parseRequest(request);
} catch (FileUploadException e1) {
return ResUtil.getInstance().getError("文件上传发生错误" + e1.getMessage(),-1);
}
Iterator it = items.iterator();
while(it.hasNext()){
FileItem fileItem = (FileItem) it.next();
if(fileItem.isFormField()){
System.out.println(fileItem.getFieldName() + "---" + fileItem.getName() + "---" + new String
(fileItem.getString().getBytes("iso8859-1"), "utf-8"));
if (fileItem.getFieldName().equals("firsttitle")) {
firsttitle=new String
(fileItem.getString().getBytes("iso8859-1"), "utf-8");//一级标题
}else if (fileItem.getFieldName().equals("twotitle")) {
twotitle=new String
(fileItem.getString().getBytes("iso8859-1"), "utf-8");//二级标题
}else if (fileItem.getFieldName().equals("newuser")) {
newuser=new String
(fileItem.getString().getBytes("iso8859-1"), "utf-8");//发布者
}else if (fileItem.getFieldName().equals("newsinfo")) {
newsinfo=new String
(fileItem.getString().getBytes("iso8859-1"), "utf-8");//新闻内容
}else if (fileItem.getFieldName().equals("newstype")) {
newstype=new String
(fileItem.getString().getBytes("iso8859-1"), "utf-8");//新闻类型
}
}else{
System.out.println(fileItem.getFieldName() + "---" +
fileItem.getName() + "---" + fileItem.isInMemory() + "---" +
fileItem.getContentType() + "---" + fileItem.getSize());
if(fileItem.getName()!=null && fileItem.getSize()!=0){
File fullFile = new File(fileItem.getName());
//重命名文件
File newFile = new File(Constants.UPLOAD.UPLOAD_URL_LOCAL +date.format(new Date())+".png");
try {
fileItem.write(newFile);
newsimg.append(";"+newFile.getName());
} catch (Exception e) {
e.printStackTrace();
}
}else{
return ResUtil.getInstance().getError("文件没有选择 或 文件内容为空",-2);
}
}
}
if (firsttitle!=null || twotitle!=null || newuser!=null || newsinfo!=null ||
newsimg!=null || newtime!=null || newstype!=null) {
News news=new News(firsttitle, twotitle,Integer.parseInt(newuser),
newsinfo, newsimg.toString(), newtime, Integer.parseInt(newstype),newscount);
newsDao.saveNews(news);
return ResUtil.getInstance().getError("新闻上传成功", 1);
}else{
return ResUtil.getInstance().getError("参数错误", 0);
}
}
return null;
}
以上代码中需要依赖包 org.apache.commons.fileupload.jar
下载地址为:资源地址:http://download.csdn.net/my
解压出相应的jar包
Constants.UPLOAD.UPLOAD_URL_LOCAL 为文件保存目录 example格式:c:/upload/img/
Constants.UPLOAD.UPLOAD_URL_DIRURL 创建的文件夹名 example格式:C:\\upload\\img
主要是依赖apache的文件上传jar来进行文件的操作进行保存。apache 很值得去研究一下,因为时间的原因代码写的很粗糙只是实现了文件的操作,没有对代码进行相应的优化。
2.文件流上传文件前端
上传前端可参照线相关的博客进行参考。android文件表单上传http://blog.csdn.net/qq_33129625/article/details/52655088