侧边栏壁纸
  • 累计撰写 781 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

文件上传

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 121 阅读 / 3,340 字
温馨提示:
本文最后更新于 2022-04-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
<form accept-charset="UTF-8" enctype="multipart/form-data" action="insertFood.do" method="post">
		<input type="file" value="" name="file" multiple="true">
		<button type="submit" >新增</button>
</form>
servlet
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.FileRenamePolicy;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//重命名策略 , 不管用 , 不知道为什么.
FileRenamePolicy renamePolicy = new MyFileRenamePolicy();
//传入文件地址, 文件就被保存到本地了. 这里可以传入重命名策略.
//保存位置在这里一次到位.
MultipartRequest m = new MultipartRequest(request, filepath, Config.maxSize);

file = m.getFile(&quot;file&quot;);

//给文件重命名
String filename = new String(file.getName().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
File newFile = new File(filepath, filename);
boolean b = file.renameTo(newFile);
file = newFile;

}




spring
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

@RequestMapping("/insertFood.do")
public String insertFood(Food food, HttpServletRequest request) throws Exception {
//将当前上下文初始化给 CommonsMutipartResolver (多部分解析器)
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//检查form中是否有enctype="multipart/form-data"
if (multipartResolver.isMultipart(request)) {
//将request变成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
//获取multiRequest 中所有的文件名
Iterator iter = multiRequest.getFileNames();
while (iter.hasNext()) {
//一次遍历所有文件
MultipartFile file = multiRequest.getFile(iter.next().toString());
if (file != null) {
path = Config.uploadImgLocation + file.getOriginalFilename();
System.out.println(path);

              //上传
              file.transferTo(new File(path));
              path = file.getOriginalFilename();
          }
      }
  }

}

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size100MB -->
<property name="maxUploadSize" value="104857600"/>
<property name="maxInMemorySize" value="4096"/>
<property name="defaultEncoding" value="utf-8"/>
</bean>





测试不管


@RequestMapping("upload")
public PictrueResult uplodPic(MultipartFile uploadFile) {
    String fileName = uploadFile.getOriginalFilename();
    fileName = UUID.randomUUID().toString() + fileName;
    File picFile = new File("D:\\shopImg\\" + fileName);
    try {
        uploadFile.transferTo(picFile);
    } catch (IOException e) {
        e.printStackTrace();
        return new PictrueResult(1, null, "上传失败");
    }
    return new PictrueResult(0, "pic\\"+fileName, "上传成功");
}

@Value("${FASTDFS-BASE-URL}")
	private String imgUrl;
	@RequestMapping("/pic/upload")
	@ResponseBody
	public String uploud(MultipartFile uploadFile){
//		public PictrueResult uploud(MultipartFile uploadFile){
		PictrueResult res = new PictrueResult();
		try {
			FastDFSClient client = new FastDFSClient("classpath:client.conf");
			String fileId = client.uploadFile(uploadFile.getBytes());
			res.setError(0);
			res.setUrl(imgUrl+fileId);
		} catch (Exception e) {
			e.printStackTrace();
			res.setError(1);
			res.setMessage("图片上传失败");
		}

// return res;
return JsonUtils.objectToJson(res);
}

0

评论区