我的项目基于maven多模块项目。
项目结构如下。 即时通讯使用jboss 7作为我的服务器。
Response
ResponseCommons
ResponseEar
ResponseModel
ResponseService
ResponseWeb
我已经完成了图片上传表格。上传工作正常,图片上传到 web 模块的资源文件夹。 问题是图像上传到jboss服务器的 tmp 文件夹,如何更改为 ResponseWeb / webapp / resources / css / image name。
当前图片保存位置
C:\jboss-as-7.1.1.Final\stand
alone\tmp\vfs\deploymenteec45ba06bd34543\ResponseWeb-1.2-SNAPSHOT.war-295
28a7e2cc4df5e\resources\css
即时通讯使用ajax表单提交上传图片。 用于上传图片的控制器
@RequestMapping(value = "/uploadImage.html", method = RequestMethod.POST)
@ResponseBody
public String uploadImageTest(@RequestParam("demoImage") MultipartFile file) throws IllegalStateException, IOException {
try {
String fileName = null;
InputStream inputStream = null;
OutputStream outputStream = null;
if (file.getSize() > 0) {
inputStream = file.getInputStream();
System.out.println("File Size:::" + file.getSize());
System.out.println("size::" + file.getSize());
fileName = request.getServletContext().getRealPath("/resources/") + "/css/"
+ file.getOriginalFilename();
outputStream = new FileOutputStream(fileName);
System.out.println("fileName:" + file.getOriginalFilename());
int readBytes = 0;
byte[] buffer = new byte[10000];
while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
outputStream.write(buffer, 0, readBytes);
}
outputStream.close();
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return "saved";
}
HTML
<form th:action="@{/school-admin/uploadImage.html}"
id="imageUploadForm" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-lg-2" style="margin-bottom: -40px;">
<div class="thumbnail">
<img id="imgStud" th:src="@{/resources/img/profile.png}"
style="width: 172px; height: 198px;" /> <br /> <input
type="file" accept="image/*" name="demoImage" id="demoImage"
onchange="fileSelected();" style="width: 170px;" />
</div>
<br />
</div>
</div>
<input type="button" class="btn btn-info pull-right"
id="btnUpload" value="upload" />
</form>
答案 0 :(得分:1)
您要保存文件的位置无效,因为它实际上是在WAR文件中,它会被分解到JBoss临时目录中,因此您会看到... / tmp / vfs / deployment ... 。文件夹。
但是,您可以通过多种方式为默认的多部分上传位置指定特定位置。
@WebServlet("/myImageFileUploader") @MultipartConfig(location = "/opt/myImageFileUploadLocation") public class MyImageFileUploaderServlet extends HttpServlet { .....}
XML配置如下
<servlet>
<servlet-name>MySpringDispatcher1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-servlet.xml</param-value>
</init-param>
<multipart-config>
<location>/opt/myImageFileUploadLocation</location>
<max-file-size>52428800</max-file-size>
<max-request-size>52428800</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>
如果您使用的是Servlet 3.0,则可以在项目中直接使用上述XML示例。为方便起见,示例代码正在配置Spring DispatcherServlet。默认情况下,JBoss 7 Web有Servlet 3.0,所以我猜它会起作用。
<bean id="myImageMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000" /> <property name="uploadTempDir" ref="myImageFileUploadDirResource" /> </bean> <bean id="myImageFileUploadDirResource" class="org.springframework.core.io.FileSystemResource"> <constructor-arg> <value>/opt/myImageFileUploadLocation</value> </constructor-arg> </bean>
希望这有帮助。