java实现文件上传,代码尽量简洁~~~·
发布网友
发布时间:2022-05-05 10:41
我来回答
共3个回答
热心网友
时间:2022-06-27 09:36
你说的2种方法都是很简单的,参考网上的资料都不难做出,用io流做更是基础中的基础,我说下smartupload好了,有的人是直接写在jsp上面,感觉比较乱,我一般都是写在action里面,打好jar包和配置后
SmartUpload mySmartUpload = new SmartUpload();
//如果是struts2.0或者webwork 则是mySmartUpload.initialize(ServletActionContext.getServletConfig(),ServletActionContext.getRequest(),ServletActionContext.getResponse());
mySmartUpload.initialize(servlet.getServletConfig(), request,response);
mySmartUpload.setTotalMaxFileSize(500000);
//如果上传任意文件不设置mySmartUpload.setAllowedFilesList(文件后缀名)就可以了
mySmartUpload.upload();
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(i);
if (file.isMissing()) continue;
file.saveAs(保存的地址 + file.getFileName(),
su.SAVE_PHYSICAL);
热心网友
时间:2022-06-27 09:37
一个SpringMVC的文件上传示例
1、jsp页面代码
<%@ page pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>上传图片</title>
</head>
<body>
<form action="message.do">
<input type="text" name="test" value="Submit" /> <input type="submit"
value="Submit" />
</form>
<form action="upload.do" method="post" enctype="multipart/form-data">
<input type="file" name="file" /> <input type="submit" value="Submit" />
</form>
</body>
</html>
2、action代码
@RequestMapping(value = "/upload.do")
public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,
ModelMap model) {
String path = "/Users/xieyuhai/Desktop";//更换一下路径
String fileName = file.getOriginalFilename();
// String fileName = new Date().getTime()+".jpg";
System.out.println(path);
File targetFile = new File(path, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("resultUrl", request.getContextPath() + "/upload/" + fileName);
return "result";
}
热心网友
时间:2022-06-27 09:37
fis = new FileInputStream(getUpload());